I have a recursive array like this
var data ={
"isRoot": true,
"isParent": true,
"name": "first",
"id": 1,
"child": [
{
"isRoot": false,
"id": 2,
"isParent": false,
"name": "second",
"child": [
{
"id": 4,
"child": [],
"name": "inner",
"isParent": false,
"isRoot": false
}
]
},
{
"isRoot": false,
"id": 3,
"isParent": true,
"name": "third",
"child": []
}
]
}
child is nested it may have in any deeper level i want to get the single object from the array that matches an id.
For eg if id=4 it should return this object
{
"id": 4,
"child": [],
"name": "inner",
"isParent": false,
"isRoot": false
}
How can I do that here what I found from the another post
function flatten(data) {
data.forEach(function (element){
//how can i do the comparison here
if(element.child.length) {
flatten(element.child);
} else {
}
});
}
flatten(data.child);
But I couldn't get an idea of matching logic does anyone have a solution for this??
childis unpredictable