Manuel Santi
Manuel Santi

Reputation: 1132

Javascript extract an array into another array and parse

In my javascript project i have an array containing another array in first position like this:

{efat: {…}, fdata: "2019-07-22", tdata: "2019-01-11"}
    efat: {50: {…}, 169: {…}, 274: {…}, 427: {…}, 490: {…}, 589:    {…}, 662: {…}, 799: {…}, 898: {…}, 994: {…}, 1085: {…}, 1293: {…}, 1407: {…}, 1500: {…}, 1573: {…}, 1647: {…}, 1705: {…}, 1817: {…}, 1922: {…}, 2060: {…}, 2064: {…}, 2065: {…}, 2150: {…}, 2251: {…}, 2348: {…}, 2481: {…}, 2568: {…}, 2703: {…}, 2809: {…}, 3023: {…}}
    fdata: "2019-07-22"
    tdata: "2019-01-11"

i try to cycle between data into first sub array like this:

$.ajax({
        type: "POST",
        url: "/f_fattnum/",
        data: {d_fatt: nfatt, d_tipo: tipo},
        success: function (data) {
            x = document.getElementById('t_fatt');
            x.parentNode.removeChild(x);

            document.getElementById('eldata').innerHTML = data.fdata+" al "+data.tdata

            //Recreata tbody and content after ajax call
            var tbody1 = document.createElement("TBODY");
            tbody1.id = "t_fatt";
            $.each(data['efat'], function (index) {

                var tr1 = document.createElement("TR");
                var td1 = document.createElement("TD");
                td1.innerHTML = data[index].fdata;

but, related to the last line of my code (in to each cycle) i receive the error:

Uncaught TypeError: Cannot read property 'fdata' of undefined

Someone can tell me how extract the subarray and parse it?

So many thanks in advance

Upvotes: 0

Views: 84

Answers (1)

Kuo-hsuan Hsu
Kuo-hsuan Hsu

Reputation: 687

I am not sure what you mean "the subarray" ? If you mean the subarray is data.efat,then try this:

$.each(data.efat, function (index, value) {   
       //index equals 50, 169, 274 and so on
       //value equals {...}, {...} and so on 
       var tr1 = document.createElement("TR");
       var td1 = document.createElement("TD");
       td1.innerHTML = data.fdata;
}

but if the subarray is meaning the value of pairs such as 50:{...} , then try this:

$.each(data.efat, function (index, value) { 
       $.each(value, function(index, value) {      
            var tr1 = document.createElement("TR");
            var td1 = document.createElement("TD");
            td1.innerHTML = data.fdata;
      })
})

Upvotes: 1

Related Questions