22 lines
614 B
TypeScript
22 lines
614 B
TypeScript
import cloneDeep from 'lodash/cloneDeep';
|
|
|
|
export function buildTree(data: any) {
|
|
const _array = cloneDeep(JSON.parse(data))
|
|
if (_array.length > 0) {
|
|
let map = new Map();
|
|
_array.forEach((item : any) => map.set(item.id, item));
|
|
_array.forEach((item : any) => {
|
|
if (item.parentId !== undefined) {
|
|
let parent = map.get(item.parentId);
|
|
if (parent) {
|
|
parent.childs.push(item);
|
|
}
|
|
}
|
|
});
|
|
|
|
return _array.filter((item : any) => !item.parentId);
|
|
} else {
|
|
return []
|
|
}
|
|
}
|