js 递归使用 遍历查找出 树形对象数组 的某特定id(1)

本文介绍了如何使用JavaScript递归遍历树形对象数组,找到`canAccess`属性为`true`的项,并将其添加到新数组中。通过示例代码展示了解决方案,并探讨了根据员工ID查找其所有下属成员的可能性。
摘要由CSDN通过智能技术生成

// console.log(‘有 children’, item.children); // 有 children 的话就调用自身

findCan(item.children) // 递归调用自身

}

})

}

findCan(arr) // 调用函数

console.log(newArr);

在这里插入图片描述



需求:找出树形数组 把 canAccess 为true的一项 添加到新数组

var arr = [

{ id: 1, canAccess: false },

{ id: 2, canAccess: true },

{ id: 3, canAccess: false, children: [{ id: 4, canAccess: true }] },

{ id: 5, canAccess: false, children: [{ id: 6, canAccess: false, children: [{ id: 7, canAccess: true }] }] },

{

id: 8, canAccess: true, children: [{

id: 9, canAccess: true,

children: [{ id: 10, canAccess: false, children: [{ id: 11, canAccess: true }] }]

}]

}

]

let newArr = []; // 用来接收新id

var findCan = function (V) {

V.forEach(item => { //

  • 25
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值