1.for循环
function toTree(arr, parentId) {
function loop(parentId) {
let res = []
for (let i = 0; i < arr.length; i++) {
let item = arr[i]
if (item.pid !== parentId) {
continue
}
item.children = loop(item.id)
res.push(item)
}
return res
}
return loop(parId)
}