Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

1.查找一个

1
2
3
4
5
6
7
8
9
10
11
12
13
let filterTreeId = (list, id)  => {
let targetObj = null;
for (const item of list) {
if (item.id === id) {
targetObj = item;
break;
}
if (item.childList && item.childList.length) {
targetObj = filterTreeId(item.childList, id);
}
}
return targetObj;
},

2.查找多个

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
let  filterTreeIdList = (List, id)  => {
let setList = [];
for (const item of List) {
if (item.id === id) {
setList.push(item);
}
if (item.childList && item.childList.length >= 1) {
setList = [
...setList,
...filterTreeIdList(item.childList, id)
];
}
}
return setList;
}

评论