Files

20 lines
588 B
TypeScript
Raw Permalink Normal View History

2024-06-17 09:55:33 +07:00
export function buildTree(data: any) {
2024-06-21 09:56:34 +07:00
const _array = JSON.parse(JSON.stringify(JSON.parse(data)))
2024-06-17 09:55:33 +07:00
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);
}
}
});
2024-06-17 13:20:31 +07:00
return _array.filter((item : any) => !item.parentId);
2024-06-17 09:55:33 +07:00
} else {
return []
}
}