1
1
# array-to-tree
2
+
2
3
[ ![ Build Status] [ travis-image ]] [ travis-url ] [ ![ Dependency Status] [ depstat-image ]] [ depstat-url ]
3
4
4
5
![ 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
9
10
10
11
## Installation
11
12
12
- ```
13
+ ``` bash
13
14
$ npm install array-to-tree --save
14
15
```
15
16
16
17
## Usage
18
+
17
19
``` js
18
20
var arrayToTree = require (' array-to-tree' );
19
21
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
+ ];
37
44
38
45
arrayToTree (dataOne);
39
46
@@ -46,23 +53,28 @@ arrayToTree(dataOne);
46
53
* About Me
47
54
*/
48
55
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
+ ];
66
78
67
79
arrayToTree (dataTwo, {
68
80
parentProperty: ' parent' ,
@@ -80,16 +92,20 @@ arrayToTree(dataTwo, {
80
92
```
81
93
82
94
## API
95
+
83
96
### ` arrayToTree(data, [options]) `
97
+
84
98
Convert a plain array of nodes (with pointers to parent nodes) to a a nested data structure.
85
99
86
100
#### Parameters
101
+
87
102
- ** Array** ` data ` : An array of data
88
103
- ** Object** ` options ` : An object containing the following fields:
89
104
- ` parentProperty ` (String): A name of a property where a link to a parent node could be found. Default: 'parent_id'
90
105
- ` customID ` (String): An unique node identifier. Default: 'id'
91
106
92
107
#### Return
108
+
93
109
- ** Array** : Result of transformation
94
110
95
111
## License
@@ -98,6 +114,5 @@ MIT © [Philipp Alferov](https://github.com/alferov)
98
114
99
115
[ travis-url ] : https://travis-ci.org/alferov/array-to-tree
100
116
[ travis-image ] : https://img.shields.io/travis/alferov/array-to-tree.svg?style=flat-square
101
-
102
117
[ depstat-url ] : https://david-dm.org/alferov/array-to-tree
103
118
[ depstat-image ] : https://david-dm.org/alferov/array-to-tree.svg?style=flat-square
0 commit comments