let data=[
{id:1,name:'部门1',pid:0},
{id:2,name:'部门6',pid:1},
{id:3,name:'部门2',pid:1},
{id:4,name:'部门3',pid:2},
{id:5,name:'部门4',pid:3},
{id:6,name:'部门5',pid:4}
]
function parseTree(arr){
let tmp={}
for(let item of arr){
tmp[item.id]=item
}
return tmp
}
function test(arr){
let root
for(let key in arr){
if(arr[key].pid===0){
root=arr[key]
}else{
let pNode=arr[arr[key].pid]
if(!pNode.children){
pNode.children=[arr[key]]
}else{
pNode.children.push(arr[key])
}
}
}
return root
}
let tmp=parseTree(data)
console.log(tmp)
let tree=test(tmp)
console.log(tree)
console