12 lines
330 B
JavaScript
12 lines
330 B
JavaScript
|
|
// 递归算法
|
||
|
|
export const initTree = (arr, parentId='0',id) => {
|
||
|
|
const tree = [];
|
||
|
|
arr.filter(item => item.parentId === parentId).forEach(item => {
|
||
|
|
const children = initTree(arr, item[id]);
|
||
|
|
if (children.length > 0) {
|
||
|
|
item.children = children;
|
||
|
|
}
|
||
|
|
tree.push(item);
|
||
|
|
});
|
||
|
|
return tree;
|
||
|
|
}
|