11# array-to-tree
2+
23[ ![ Build Status] [ travis-image ]] [ travis-url ] [ ![ Dependency Status] [ depstat-image ]] [ depstat-url ]
34
45![ array-to-tree] ( media/array-to-tree.png )
@@ -9,31 +10,37 @@ Solves a problem with conversion of retrieved from a database sets of data to a
910
1011## Installation
1112
12- ```
13+ ``` bash
1314$ npm install array-to-tree --save
1415```
1516
1617## Usage
18+
1719``` js
1820var arrayToTree = require (' array-to-tree' );
1921
20- var dataOne = [{
21- id: 1 ,
22- name: " Portfolio" ,
23- parent_id: undefined
24- }, {
25- id: 2 ,
26- name: " Web Development" ,
27- parent_id: 1
28- }, {
29- id: 3 ,
30- name: " Recent Works" ,
31- parent_id: 2
32- }, {
33- id: 4 ,
34- name: " About Me" ,
35- parent_id: undefined
36- }];
22+ var dataOne = [
23+ {
24+ id: 1 ,
25+ name: ' Portfolio' ,
26+ parent_id: undefined
27+ },
28+ {
29+ id: 2 ,
30+ name: ' Web Development' ,
31+ parent_id: 1
32+ },
33+ {
34+ id: 3 ,
35+ name: ' Recent Works' ,
36+ parent_id: 2
37+ },
38+ {
39+ id: 4 ,
40+ name: ' About Me' ,
41+ parent_id: undefined
42+ }
43+ ];
3744
3845arrayToTree (dataOne);
3946
@@ -46,23 +53,28 @@ arrayToTree(dataOne);
4653 * About Me
4754 */
4855
49- var dataTwo = [{
50- _id: ' ec654ec1-7f8f-11e3-ae96-b385f4bc450c' ,
51- name: " Portfolio" ,
52- parent: null
53- }, {
54- _id: ' ec666030-7f8f-11e3-ae96-0123456789ab' ,
55- name: " Web Development" ,
56- parent: ' ec654ec1-7f8f-11e3-ae96-b385f4bc450c'
57- }, {
58- _id: ' ec66fc70-7f8f-11e3-ae96-000000000000' ,
59- name: " Recent Works" ,
60- parent: ' ec666030-7f8f-11e3-ae96-0123456789ab'
61- }, {
62- _id: ' 32a4fbed-676d-47f9-a321-cb2f267e2918' ,
63- name: " About Me" ,
64- parent: null
65- }];
56+ var dataTwo = [
57+ {
58+ _id: ' ec654ec1-7f8f-11e3-ae96-b385f4bc450c' ,
59+ name: ' Portfolio' ,
60+ parent: null
61+ },
62+ {
63+ _id: ' ec666030-7f8f-11e3-ae96-0123456789ab' ,
64+ name: ' Web Development' ,
65+ parent: ' ec654ec1-7f8f-11e3-ae96-b385f4bc450c'
66+ },
67+ {
68+ _id: ' ec66fc70-7f8f-11e3-ae96-000000000000' ,
69+ name: ' Recent Works' ,
70+ parent: ' ec666030-7f8f-11e3-ae96-0123456789ab'
71+ },
72+ {
73+ _id: ' 32a4fbed-676d-47f9-a321-cb2f267e2918' ,
74+ name: ' About Me' ,
75+ parent: null
76+ }
77+ ];
6678
6779arrayToTree (dataTwo, {
6880 parentProperty: ' parent' ,
@@ -80,16 +92,20 @@ arrayToTree(dataTwo, {
8092```
8193
8294## API
95+
8396### ` arrayToTree(data, [options]) `
97+
8498Convert a plain array of nodes (with pointers to parent nodes) to a a nested data structure.
8599
86100#### Parameters
101+
87102- ** Array** ` data ` : An array of data
88103- ** Object** ` options ` : An object containing the following fields:
89104 - ` parentProperty ` (String): A name of a property where a link to a parent node could be found. Default: 'parent_id'
90105 - ` customID ` (String): An unique node identifier. Default: 'id'
91106
92107#### Return
108+
93109- ** Array** : Result of transformation
94110
95111## License
@@ -98,6 +114,5 @@ MIT © [Philipp Alferov](https://github.com/alferov)
98114
99115[ travis-url ] : https://travis-ci.org/alferov/array-to-tree
100116[ travis-image ] : https://img.shields.io/travis/alferov/array-to-tree.svg?style=flat-square
101-
102117[ depstat-url ] : https://david-dm.org/alferov/array-to-tree
103118[ depstat-image ] : https://david-dm.org/alferov/array-to-tree.svg?style=flat-square
0 commit comments