- 将
[{id:1},{id:2, pId:1},...]
转成[id1:child:[{id:2, pid:1}]]
javascript
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;
}