tree-to-list
v3.0.2
Published
Flatten tree to list
Downloads
904
Readme
tree-to-list
Flatten tree to list
Install
$ npm install tree-to-list
Usage
const treeToList = require('tree-to-list');
// flatten array tree
const arrayTree = [{
name: 'name1',
children: [{
name: 'name3',
children: [{
name: 'name5'
}]
}, {
name: 'name4'
}]
}, {
name: 'name2'
}];
console.log(treeToList(arrayTree, 'children'));
/*
[
{ name: 'name1' },
{ name: 'name3' },
{ name: 'name5' },
{ name: 'name4' },
{ name: 'name2' }
]
*/
// flatten object tree
const objectTree = {
node1: {
name: 'name1',
tree: {
node3: {
name: 'name3',
tree: {
node2: {
name: 'name5',
key5: 'value5'
}
}
},
node4: { name: 'name4' },
}
},
node2: {
name: 'name2',
key2: 'value2'
}
};
console.log(treeToList(objectTree, 'tree'));
/*
{
node1: { name: 'name1' },
node3: { name: 'name3' },
node4: { name: 'name4' },
node2: {
name: 'name2',
key5: 'value5',
key2: 'value2'
}
}
*/
API
treeToList(tree, key)
Returns flattened Array
or Object
list.
tree
Type: Array
or Object
key
Type: String
Default: 'children'
The key of child nodes.
License
MIT