mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 21:30:31 +00:00
25 lines
502 B
TypeScript
25 lines
502 B
TypeScript
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;
|
|
};
|