猿问

vue递归查找id对应的对象

将递归得到的对象push到数组中
letselections=[];
for(letleafofthis.tree){
letresult=this.recursion(this.items,leaf);
selections.push(result);
}
递归函数
recursion(data,id){
letresult;
if(!data){
return;
}
for(vari=0;iletitem=data[i];
if(item.id===id){
result=item;
break;
}elseif(item.children&&item.children.length>0){
result=this.recursion(item.children,id);
}
}
console.log(result);
returnresult;
},
this.items为树结构的数据
items:[
{
id:1,
name:'VuetifyHumanResources',
children:[
{
id:2,
name:'Coreteam',
children:[
{
id:201,
name:'John'
},
{
id:202,
name:'Kael'
},
{
id:203,
name:'Nekosaur'
},
{
id:204,
name:'Jacek'
},
{
id:205,
name:'Andrew'
}
]
},
{
id:3,
name:'Administrators',
children:[
{
id:301,
name:'Ranee'
},
{
id:302,
name:'Rachel'
}
]
},
{
id:4,
name:'Contributors',
children:[
{
id:401,
name:'Phlow'
},
{
id:402,
name:'Brandon'
},
{
id:403,
name:'Sean'
}
]
}
]
},
{
id:5,
name:'Example',
children:[
{
id:501,
name:'qaz'
}
]
},
{
id:6,
name:'Qdd',
children:[
{
id:601,
name:'yui'
}
]
}
],
tree为树选中的数据[201,202,203]在递归中我想要得到id所在的对象,在第一个方法中push到数组中形成一个数组集合,现在递归方法出现了问题得到的result为undefined,请各位帮我看下?
千巷猫影
浏览 1308回答 2
2回答
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答