array-to-tree 
Convert a plain array of nodes (with pointers to parent nodes) to a tree.
Solves a problem with conversion of retrieved from a database sets of data to a nested structure (i.e. navigation tree).
Install
$ npm install --save array-to-tree
Usage
Basic
var arrayToTree = require('array-to-tree');
var data = [{
id: 1,
name: "Portfolio",
parent_id: null
}, {
id: 2,
name: "Web Development",
parent_id: 1
}, {
id: 3,
name: "Recent Works",
parent_id: 2
}, {
id: 4,
name: "About Me",
parent_id: null
}];
var tree = arrayToTree({ data: data });
With Custom Attributes
var arrayToTree = require('array-to-tree');
var data = [{
_id: 'ec654ec1-7f8f-11e3-ae96-b385f4bc450c',
name: "Portfolio",
parent: null
}, {
_id: 'ec666030-7f8f-11e3-ae96-0123456789ab',
name: "Web Development",
parent: 'ec654ec1-7f8f-11e3-ae96-b385f4bc450c'
}, {
_id: 'ec66fc70-7f8f-11e3-ae96-000000000000',
name: "Recent Works",
parent: 'ec666030-7f8f-11e3-ae96-0123456789ab'
}, {
_id: '32a4fbed-676d-47f9-a321-cb2f267e2918',
name: "About Me",
parent: null
}];
var tree = arrayToTree({
parentProperty: 'parent',
customID: '_id'
data: data
});
API
arrayToTree(options)
Convert a plain array of nodes (with pointers to parent nodes) to a tree.
Params
Object options
: An object containing the following fields:
parentProperty
(String): A name of a property where a link to a parent node could be found. Default: 'parent_id'
data
(Array): An array of` data
customID
(String): An unique node identifier. Default: 'id'
Return
- Array: Result of transformation
License
MIT © Philipp Alferov