有没有什么快捷一点的方法去遍历树形结构的数组,取出需要的值呢?var arr = [ { title: '书籍', id: 1, select: false, children: [ { title: '语文', id: 11, select: false }, { title: '数学', id: 12, select: false } ] }, { title: '文具', id: 2, select: true, children: [ { title: '铅笔', id: 21, select: false }, { title: '尺子', id: 12, select: false } ] }, { title: '水果', id: 3, select: false, children: [ { title: '苹果', id: 31, select: true }, { title: '香蕉', id: 32, select: false } ] } ];取出select为true的值
6 回答
慕无忌1623718
TA贡献1744条经验 获得超4个赞
var res = []
var len = arr.length;
for (let i = 0; i < len; i++){
if (arr[i].select) res.push(arr[i])
var childLen = arr[i].children.length
for (let j = 0; j < childLen; j++) {
if (arr[i].children[j].select) res.push(arr[i].children[j])
}
}
献丑了
添加回答
举报
0/150
提交
取消