const obj =
{
name:"1",
id:1,
child:[
{
name:2,
id:2,
child:[
{
name:4,
id:4,
}
]
},
{
name:3,
id:3,
},
{
name:5,
id:5,
}
]
}
// function recursionGet(setValues, valueId) {
// let value = null;
// for (let index = 0; index < setValues.length; index += 1) {
// if (setValues[index].value === valueId) {
// value = setValues[index].label;
// break;
// }
// if (setValues[index].children instanceof Array && setValues[index].children.length > 0) {
// const text = recursionGet(setValues[index].children, valueId);
// if (text)
// return text;
// }
// }
// return value;
// }
// 递归查找 便利子树
function deep(obj,targetId){
let value = null;
if(obj.id == targetId){
const {id,name} = obj;
value = {
id,name
}
return value;
}
if(obj.child){
for(let i = 0;i<obj.child.length;i++){
const value = deep(obj.child[i],targetId);
if(value){
return value
break;
};
}
}
}
console.log('deep(obj,targetId)',deep(obj,4))