Probably a simple question but I can't figure it out. I have an nested object in javascript like this and i have function for find object in object.
[
{
"id": "general",
"components": [
{
"id": "na17ea10-e126",
"parentId": "general",
"type": "L6",
"components": [
{
"id": "n5c4fd4a-5fe0",
"parentId": "na17ea10-e126",
"type": "L6E",
"components": [
{
"id": "n0f17d51-143e",
"parentId": "n5c4fd4a-5fe0",
"type": "Text",
"components": []
}
]
},
{
"id": "n7e86ff5-cfd2",
"parentId": "na17ea10-e126",
"type": "L6E",
"components": []
}
]
}
]
}
]
and I have a search function
findInSchema(o, id){
let i;
for (i of o) {
if(i.id==id){
return i;
}else if(i.components.length>0){
return findInSchema(i.components,id);
}
}
}
as in the above object example;
findInSchema(obj, 'n5c4fd4a-5fe0')
give me
{
"id": "n5c4fd4a-5fe0",
"parentId": "na17ea10-e126",
"type": "L6E",
"components": [
{
"id": "n0f17d51-143e",
"parentId": "n5c4fd4a-5fe0",
"type": "Text",
"components": []
}
]
}
but in the other example on the same layer
findInSchema(obj, 'n7e86ff5-cfd2')
give me 'undefined'
componentsarrays and never consider the remaining ones.