JSFiddle - React, Tailwind, and code Playground
by Guddu Kumar
JavaScript
var data = {
"items": [
{
"id": "d1"
},
{
"id": "2",
"children": [
{
"id": "3"
},
{
"id": "4"
},
{
"id": "5",
"children": [
{
"id": "6"
},
{
"id": "7",
"name": "test7",
"children": [
{
"id": "8"
},
{
"id": "9"
}
]
},
{
"id": "10"
}
]
},
{
"id": "11"
},
{
"id": "12"
}
]
},
{
"id": "13"
},
{
"id": "14"
}
]
}
function find(source, id)
{
for (key in source)
{
var item = source[key];
if (item.id == id)
return item;
if (item.children)
{
var subresult = find(item.children, id);
if (subresult)
return subresult;
}
}
return null;
}
var result = find(data.items, 6);
alert(result.name);