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
asked Mar 23, 2020 at 7:40
Manuel Santi
1,1521 gold badge20 silver badges54 bronze badges
1 Answer 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;
})
})
answered Mar 23, 2020 at 8:32
Kuo-hsuan Hsu
6871 gold badge6 silver badges12 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js
data.efat[index].fdata.[<>]button and create a minimal reproducible example with proper arrays/obejcts and HTML