Reputation: 1044
If I have a nested array
as follows:
var attendees={attendees :[{name: John},{name: Terry}]}
how do I loop through the names using the forEach
function? I have tried:
attendees.forEach(function(attendees){
console.log(attendees.name):
});
but it does not loop through the subarrays and just gives me:
[{name: John},{name: Terry}]
Appreciate the help!
Upvotes: 0
Views: 4772
Reputation: 2022
It should be like this :
var attendees={
attendees : [
{ name: 'John'},
{ name: 'Terry'}
]};
attendees.attendees.forEach(function(attendees){console.log(attendees.name);});
Upvotes: 1
Reputation: 22158
With another foreach when detects is an array:
attendees.forEach(function(attendee){
if(attendee.isArray()) {
attendee.forEach(function(subattendee) {
console.log(subattendee):
});
}
});
Be carefull with the nested variable. You have the same name for de readed array and for the function asigned variable.
Good luck
Upvotes: 2