flatten-tree
v0.0.3
Published
Transforms a nested tree structure into a flat list.
Downloads
65
Readme
flatten-tree
Transforms a nested tree structure into a flat list.
Install
$ npm i flatten-tree
Use
import flattenTree from 'flatten-tree';
const tree = [
{
name: 'item1',
children: [
{
name: 'item2',
children: [
{name: 'item3'}
]
},
{name: 'item4'}
]
}
];
const options = {
initNode: node => node, // <= default, consider node => _.clone(node) to avoid mutating the tree
};
const list = flattenTree(tree, 'children', options);
Results in:
[
{id: 1, name: 'item1', children: [2, 4]},
{id: 2, name: 'item2', children: [3]},
{id: 3, name: 'item3'},
{id: 4, name: 'item4'}
]
Test
npm test