let arr = [
{ id: 1, name: "部门1", pid: 0 },
{ id: 2, name: "部门2", pid: 1 },
{ id: 3, name: "部门3", pid: 1 },
{ id: 4, name: "部门4", pid: 3 },
{ id: 5, name: "部门5", pid: 6 }
];
const findtree = list => {
let root: any = [];
for (let i = 0; i < list.length; i++) {
const parent = list.find(v => v.id === list[i].pid);
if (parent) {
parent.children
? parent.children.push(list[i])
: (parent.children = [list[i]]);
} else {
root.push(list[i]);
}
}
return root;
};