export function toTree (list, idProp, parentIdProp) {
const map = new Map()
const tree = []
// 将列表中的每个项映射到哈希表中
list.forEach((item) => {
map.set(item[idProp], item)
})
// 遍历列表,将每个项添加到父级的 children 数组中
list.forEach((item) => {
const parent = map.get(item[parentIdProp])
if (parent) {
if (!parent.children) {
parent.children = []
}
parent.children.push(item)
} else {
tree.push(item)
}
})
return tree
}
浅拷贝,直接更改了item 的值