array-to-tree-js
v1.0.4
Published
simple create nested tree from array
Downloads
20
Readme
array-to-tree-js
simple create nested tree from array
Installation
$ npm i array-to-tree-js -P
Usage
import to_tree from 'array-to-tree-js';
let dataOne = [{
id: 1,
name: "Portfolio",
parent_id: undefined // or 0
}, {
id: 2,
name: "Web Development",
parent_id: 1
}, {
id: 3,
name: "Recent Works",
parent_id: 2
}, {
id: 4,
name: "About Me",
parent_id: 0
}];
console.log(to_tree(dataOne, {id_parent: 'parent_id', ID: 'id'}));
/*
* Output:
*
* Portfolio
* Web Development
* Recent Works
* About Me
*/
API
array-to-tree-js(data, [options])
Convert a plain array of nodes (with pointers to parent nodes) to a a nested data structure.
Parameters
- Array
data
: An array of data - Object
options
: An object containing the following fields:id_parent
(String): A name of a property where a link to a parent node could be found. Default: 'id_parent'ID
(String): An unique node identifier. Default: 'id_group'
Return
- Array: Result of transformation
License
MIT © Serhiy Popaduk