0

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

4
  • 1
    It should be data.efat[index].fdata. Commented Mar 23, 2020 at 7:45
  • what is the data format of data['efat']. Is it an array or an object Commented Mar 23, 2020 at 7:54
  • If this is you actual Json, efat is not an array. Besides that, why not use vanilla JS forEach later on? Commented Mar 23, 2020 at 7:55
  • 1
    Suggestion: If the AJAX works, don't include it. Instead click edit, then the [<>] button and create a minimal reproducible example with proper arrays/obejcts and HTML Commented Mar 23, 2020 at 7:56

1 Answer 1

1

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;
      })
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.