I'm currently trying to build a function that traverse through a nested object looking for a value that matches. I created this custom code that checks each levels of object. The problem how can repeat the function multiple times until it fetches or match with the value I'm looking for traversing to multiple levels.
let animalTree = {
"animals":[
{
"species":"Vertebrates",
"types": [
{
"category": "Fish",
},
{
"category": "Amphibians",
},
{
"category": "Reptiles",
},
{
"category": "Birds",
},
{
"category": "Mammals",
}
]
},
{
"species":"Invertebrates",
},
{
"species":"Annelids",
},
{
"species":"Molluscs",
},
{
"species":"Nematodes",
},
{
"species":"Arthropods",
},
],
}
let scanTree = () => {
let matchValue = "Vertebrates";
for (let i = 0; i < animalTree.length; i++){
if (animalTree[i].species == matchValue){
console.log('Found')
}
}
}
let jParse = JSON.stringify(animalTree);
scanTree();
categoryofspeciewhy do you want to run your function recursively ?animalsobject? Your question is a little confusing - you shouldn't need to write your own function for this..