Possible Duplicate:
How do I enumerate the properties of a javascript object?
{
347655082: {
album:{
title: "A",
genre: "Pop",
}
},
347655083: {
album:{
title: "B",
genre: "Rock",
}
}
}
Normally the “outside” key is the same, so I can easily target the nested objects.
In the case the “outside” key is variable which can be anything.
albums = JSON.parse(json); //parse json storing it in albums
I cannot run a foreach on albums, say “albums has not method foreach”.
albums.forEach(function(album, i){
}
You can only use
.forEach()on arrays. Youralbumsentity is anObjectso you should usefor ... in ...)For code targetting node.js or any other ES5 implementation you could probably omit the
ifclause – it’s only needed if you’ve got code that has unsafely added stuff toObject.prototype.