consttree={id:"root",children:[{id:1,children:['id1']},{id:2,children:['id2']},{id:3,children:['id3']},{id:4,children:[{id:5,children:['id4的id5']}]},{id:6,children:[{id:7,children:[{id:8,children:['id4的id5']}]}]}]}functionsearch(id,arr){letasd={};arr.forEach((val,ind,arr)=>{if(!val.id){asd='meiyou';return}if(val.id==id){asd['id']=val.id;asd['children']=val.children;console.log(asd);//这里打印的值是正确的return;}else{search(id,val.children);}});returnasd;}varresult=search(5,tree.children);console.log(result);//为空{}数组的第一层可以正常返回,更深一点的就返回时{},求问正确答案
添加回答
举报
0/150
提交
取消