I have an tree structured object e.g
var x = [{
id: 1,
children: [{
id: 11,
children: [],
}, {
id: 12,
children: [{
id: 121,
name:'jogn',
children: []
}]
}]
}, {
id: 2,
children: [],
}]
And i would like to find object with specific ID in it. I made
function printObj(obj , val) {
for( var i = 0; i < obj.length ; i++){
if( obj[i].id == val){
return obj[i];
}
if( obj[i].children.length > 0 ){
printObj( obj[i].children)
}
}
}
function. The problem is when i invoke it
var g = printObj(x , 121);
alert(x.name)
it returns undefined instead of jogn altought when i pop some alert if it findes set value it does find it . Why is it returning wrong object then?
g.nameinstead ofx?