You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
16 lines
395 B
16 lines
395 B
9 years ago
|
export function flatArray(data = [], childrenName = 'children') {
|
||
|
const result = [];
|
||
|
const loop = (array) => {
|
||
|
array.forEach(item => {
|
||
|
const newItem = { ...item };
|
||
|
delete newItem[childrenName];
|
||
|
result.push(newItem);
|
||
|
if (item[childrenName] && item[childrenName].length > 0) {
|
||
|
loop(item[childrenName]);
|
||
|
}
|
||
|
});
|
||
|
};
|
||
|
loop(data);
|
||
|
return result;
|
||
|
}
|