In the following code, I have an array which contains objects which each contain another object. I'd like to do a for loop which targets one of the elements inside the innermost object.
var myArray = [
{nemo : {type: "fish", scales: "yes"}},
{bubbles : {type: "fish", scales: "yes"}},
{jimmy : {type: "turtle", scales: "no"}}
];
function findType (array, animalType) {
var newArray = [];
for (i = 0; i < array.length; i ++){
if (array[i][*what goes here?*].type == animalType){
newArray.push(array[i]);
}
}
return newArray;
}
findType(myArray, "fish");
The problem I'm having is to get down to the level I want the names of the fish (i.e. nemo, bubbles) would need to be known in each loop of the for loop, and these names are different each time. What would be a way to go down past this level into the object that I want? Thank you.