I have some data that is in JSON object array. I\'m trying to use nested forEach loops to extract the data.
The data is modeled like belo. There\'s multiple dataModels
You are not targeting the array inside the entry
object, you need to loop over the childNodes
property in order to get the data you want. See example below.
var dataModels = [];
dataModels[0] = {
childNodes: []
};
dataModels[0].childNodes[0] = {
appId: "foo"
};
dataModels.forEach(function(entry){
entry.childNodes.forEach(function(childrenEntry) { // was missing a )
console.log(childrenEntry.appId);
});
});
JsFiddle demo
Nesting foreach is really a bad practice. Instead of that you can use the map() function to get data.
Suppose a array of object be like this & now here how to use map instead of multiple foreach();
data = [{
dataModels: [{
childNodes: {
appId: 'foo'
}
}]
}];
data.forEach(function(obj) {
var res = obj.dataModels.map(function(o) {
return o.childNodes;
});
console.log(res[0]);
});
It appears to me that your solution is correct, but you're missing a parentheses and you're not referencing the childNodes attribute:
data.forEach(function(entry){
entry.childNodes.forEach(function(childrenEntry){
console.log(childrenEntry.appId);
})
})