手写代码之数组转tree
function listToTree(list, parentId = null) { const tree = []; for (let i = 0; i < list.length; i++) { if (list[i].parentId === parentId) { const node = { id: list[i].id, name: list[i].name, children: listToTree(list, list[i].id), }; tree.push(node); } } return tree; }