data() {
return {
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: 4
}
]
}
},
mounted() {
const tree = this.arr.filter((father) => {
let brancharr = this.arr.filter((child) => {
return father.id === child.pid
})
if (brancharr.length > 0) {
father.children = brancharr
}
return father.pid == 0
})
console.log('tree', tree)
}