如何正确的使用递归?

需求是找到对应code第一次出现的位置,一下函数始终不能正确执行,大家帮忙改正下
letmenus=[{
code:2,
name:'222',
children:[
{
code:33,
name:'222sa',
children:[
{
code:44,
name:'22'
}
]
}
]
},{
code:922,
name:'922',
children:[
{
code:9222,
name:'92222222dsadsa',
children:[
{
code:44222,
name:'442222dsads'
}
]
}
]
}]
functiondigui(arr,code){
for(vari=0;iif(arr[i].code==code){
returnarr[i]
}else{
if(arr[i].children){
digui(arr[i].children,code)
}
}
}
}
console.log(digui(menus,44222))
九州编程
浏览 376回答 2
2回答

慕盖茨4494581

functiondigui(arr,code){for(vari=0;i

尚方宝剑之说

letchildCode=digui(item.children,code);if(childCode){returnchildCode}需要对递归函数返回值做一下处理
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript