树形数据结构转换(未完成)

  1. [{id:1},{id:2, pId:1},...]转成[id1:child:[{id:2, pid:1}]]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const fn = arr => {
const res = []
const map = arr.reduces((res, item) => ((res[item.id] = item), res), {})
for (const item of Object.values(map)) {
it(!item.pId) {
res.push(item)
} else {
const parent = map[item.pId]
parent.child = patent.child || []
parent.child.push(item)
}
}
return res;
}