lets say I have a JSON/object literal like so:
var animals = {
dogs : {
breed : 'breed name'
weight : 'fat'
},
cats : {
breed : 'breed name'
weight : 'fat'
}
}
because dogs and cats are the same, is there a way to abstract that key? So for example if you were doing a loop on animals, how would you have a single loop and get to breed and weight of dogs and cats with out actually using dogs and cats? The JSON I have goes a couple levels deeper than this before this happens.
Thanks for any help!
Yes it is possible. You can loop over the keys of the object.
for(var animalName in animals) {
currentAnimal=animals[animalName];
console.log(animalName,currentAnimal);
}
That will loop through all your animals and print the name and the associated object.
Just FYI: Your JSON is invalid, you need to add commas between the properties