1.查找一个
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.查找多个
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;
}
网友评论