const list = [
{id:1,name:'一级 1',pid:0},
{id:2,name:'二级 1-1',pid:1},
{id:3,name:'二级 1-2',pid:1},
{id:4,name:'三级 1-1-1',pid:2},
{id:5,name:'一级 2',pid:0},
]
function listToTree(list){
return list.filter(e => {
let pid = e.pid;
let newArr = list.filter(ele => {
if(ele.id == pid){
if(!ele.children){
ele.children = [];
}
ele.children.push(e);
return true
}
});
return newArr.length===0
});
}
listToTree(list);
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。