我的解决方案
Liboq opened this issue · 0 comments
Liboq commented
const listToTree = (rootList, id = 0, list = []) => {
for (const item in rootList) {
if (rootList[item].pid === id) {
list.push(rootList[item]);
rootList.splice(item, 1);
}
}
for (const i of list) {
i.children = [];
const children = rootList.filter((item) => {
return item.pid === i.id;
});
children.forEach((item) => {
item.children = [];
const list1 = listToTree(rootList, item.id, list.children);
item.children.push(...list1);
});
i.children.push(...children);
}
return list;
};