mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 12:20:31 +00:00
34 lines
754 B
TypeScript
34 lines
754 B
TypeScript
import { omit, concat } from 'lodash';
|
|
|
|
export const nestedArrayToFlatten = (
|
|
collection,
|
|
property = 'children',
|
|
parseItem = (a, level) => a,
|
|
level = 1,
|
|
) => {
|
|
const parseObject = (obj) =>
|
|
parseItem(
|
|
{
|
|
...omit(obj, [property]),
|
|
},
|
|
level,
|
|
);
|
|
|
|
return collection.reduce((items, currentValue, index) => {
|
|
let localItems = [...items];
|
|
const parsedItem = parseObject(currentValue);
|
|
localItems.push(parsedItem);
|
|
|
|
if (Array.isArray(currentValue[property])) {
|
|
const flattenArray = nestedArrayToFlatten(
|
|
currentValue[property],
|
|
property,
|
|
parseItem,
|
|
level + 1,
|
|
);
|
|
localItems = concat(localItems, flattenArray);
|
|
}
|
|
return localItems;
|
|
}, []);
|
|
};
|