File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed
Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ # array-to-tree
2+
3+ > Convert a plain array of nodes (with pointers to parent nodes) to a tree.
4+
5+ Solves a problem with conversion of retrieved from a database sets of data to a nested structure (i.e. navigation tree).
6+
7+ ## Install
8+
9+ ```
10+ $ npm install --save array-to-tree
11+ ```
12+
13+ ## Usage
14+
15+ ``` js
16+ var arrayToTree = require (' array-to-tree' );
17+
18+ var navigation = [{
19+ id: 1 ,
20+ name: " Portfolio" ,
21+ parent_id: null
22+ }, {
23+ id: 2 ,
24+ name: " Web Development" ,
25+ parent_id: 1
26+ }, {
27+ id: 3 ,
28+ name: " Recent Works" ,
29+ parent_id: 2
30+ }, {
31+ id: 4 ,
32+ name: " About Me" ,
33+ parent_id: null
34+ }];
35+
36+ var navigationTree = arrayToTree (navigation);
37+
38+ /*
39+ * Output:
40+ * Portfolio
41+ * Web Development
42+ * Recent Works
43+ * About Me
44+ */
45+
46+ ```
47+
48+ ## License
49+
50+ MIT
51+ [ Philipp Alferov] ( https://github.com/alferov )
You can’t perform that action at this time.
0 commit comments