mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-20 06:40:31 +00:00
refactor: wip migrate ot nestjs
This commit is contained in:
24
packages/server-nest/src/utils/flat-to-nested-array.ts
Normal file
24
packages/server-nest/src/utils/flat-to-nested-array.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
export const flatToNestedArray = (
|
||||
data,
|
||||
config = { id: 'id', parentId: 'parent_id' },
|
||||
) => {
|
||||
const map = {};
|
||||
const nestedArray = [];
|
||||
|
||||
data.forEach((item) => {
|
||||
map[item[config.id]] = item;
|
||||
map[item[config.id]].children = [];
|
||||
});
|
||||
|
||||
data.forEach((item) => {
|
||||
const parentItemId = item[config.parentId];
|
||||
|
||||
if (!item[config.parentId]) {
|
||||
nestedArray.push(item);
|
||||
}
|
||||
if (parentItemId) {
|
||||
map[parentItemId].children.push(item);
|
||||
}
|
||||
});
|
||||
return nestedArray;
|
||||
};
|
||||
Reference in New Issue
Block a user