let result=[]
let flag=true
function findPath (array) {
array.map(item => {
if(flag){
if (item.children) {
result.push(item.path) //将路径保存到数组中
findPath(item.children); //继续递归
} else { //找到了没有孩子的对象,将flag设置为false,不再继续递归,返回result数组
result.push(item.path)
flag=false
return result
}
}
})
}
let array = [
{
path: '111', children: [
{ path: 'operator1', children: [{ path: 'operator' }] },
{path:'operator2'}
]
},
{path:'222',children:[{path:'9999'}]}
]
findPath(array)
console.log(result);
递归找到第一个没有孩子的对象,并得到路径
最新推荐文章于 2024-07-26 15:05:55 发布