7
7
[ ![ Dependency Status] ( https://david-dm.org/philipstanislaus/performant-array-to-tree.svg )] ( https://david-dm.org/philipstanislaus/performant-array-to-tree )
8
8
[ ![ devDependency Status] ( https://david-dm.org/philipstanislaus/performant-array-to-tree/dev-status.svg )] ( https://david-dm.org/philipstanislaus/performant-array-to-tree?type=dev )
9
9
[ ![ typings included] ( https://img.shields.io/badge/typings-included-brightgreen.svg )] ( #typescript )
10
- [ ![ JavaScript Style Guide] ( https://img.shields.io/badge/code_style-standard-brightgreen.svg )] ( https://standardjs.com )
11
10
[ ![ npm license] ( https://img.shields.io/npm/l/performant-array-to-tree.svg )] ( https://www.npmjs.com/package/performant-array-to-tree )
12
11
13
12
Converts an array of items with ids and parent ids to a nested tree in a performant way (time complexity ` O(n) ` ). Runs in browsers and node.
@@ -33,26 +32,32 @@ or if using npm
33
32
34
33
``` js
35
34
const tree = arrayToTree ([
36
- { id: ' 4 ' , parentId: null , custom: ' abc' },
37
- { id: ' 31 ' , parentId: ' 4 ' , custom: ' 12 ' },
38
- { id: ' 1941' , parentId: ' 418' , custom: ' de ' },
39
- { id: ' 1 ' , parentId: ' 418' , custom: ' ZZZz' },
40
- { id: ' 418' , parentId: null , custom: ' ü ' },
41
- ])
35
+ { id: " 4 " , parentId: null , custom: " abc" },
36
+ { id: " 31 " , parentId: " 4 " , custom: " 12 " },
37
+ { id: " 1941" , parentId: " 418" , custom: " de " },
38
+ { id: " 1 " , parentId: " 418" , custom: " ZZZz" },
39
+ { id: " 418" , parentId: null , custom: " ü " },
40
+ ]);
42
41
```
43
42
44
43
Which results in the following array:
45
44
46
45
``` js
47
46
[
48
- { data: { id: ' 4' , parentId: null , custom: ' abc' }, children: [
49
- { data: { id: ' 31' , parentId: ' 4' , custom: ' 12' }, children: [] },
50
- ] },
51
- { data: { id: ' 418' , parentId: null , custom: ' ü' }, children: [
52
- { data: { id: ' 1941' , parentId: ' 418' , custom: ' de' }, children: [] },
53
- { data: { id: ' 1' , parentId: ' 418' , custom: ' ZZZz' }, children: [] },
54
- ] },
55
- ]
47
+ {
48
+ data: { id: " 4" , parentId: null , custom: " abc" },
49
+ children: [
50
+ { data: { id: " 31" , parentId: " 4" , custom: " 12" }, children: [] },
51
+ ],
52
+ },
53
+ {
54
+ data: { id: " 418" , parentId: null , custom: " ü" },
55
+ children: [
56
+ { data: { id: " 1941" , parentId: " 418" , custom: " de" }, children: [] },
57
+ { data: { id: " 1" , parentId: " 418" , custom: " ZZZz" }, children: [] },
58
+ ],
59
+ },
60
+ ];
56
61
```
57
62
58
63
## Configuration
@@ -69,80 +74,107 @@ You can provide a second argument to arrayToTree with configuration options. Rig
69
74
Example:
70
75
71
76
``` js
72
- const tree = arrayToTree ([
73
- { num: ' 4' , ref: null , custom: ' abc' },
74
- { num: ' 31' , ref: ' 4' , custom: ' 12' },
75
- { num: ' 1941' , ref: ' 418' , custom: ' de' },
76
- { num: ' 1' , ref: ' 418' , custom: ' ZZZz' },
77
- { num: ' 418' , ref: null , custom: ' ü' },
78
- ], { id: ' num' , parentId: ' ref' , childrenField: ' nodes' })
77
+ const tree = arrayToTree (
78
+ [
79
+ { num: " 4" , ref: null , custom: " abc" },
80
+ { num: " 31" , ref: " 4" , custom: " 12" },
81
+ { num: " 1941" , ref: " 418" , custom: " de" },
82
+ { num: " 1" , ref: " 418" , custom: " ZZZz" },
83
+ { num: " 418" , ref: null , custom: " ü" },
84
+ ],
85
+ { id: " num" , parentId: " ref" , childrenField: " nodes" }
86
+ );
79
87
```
80
88
81
89
Which produces:
82
90
83
91
``` js
84
92
[
85
- { data: { num: ' 4' , ref: null , custom: ' abc' }, nodes: [
86
- { data: { num: ' 31' , ref: ' 4' , custom: ' 12' }, nodes: [] },
87
- ] },
88
- { data: { num: ' 418' , ref: null , custom: ' ü' }, nodes: [
89
- { data: { num: ' 1941' , ref: ' 418' , custom: ' de' }, nodes: [] },
90
- { data: { num: ' 1' , ref: ' 418' , custom: ' ZZZz' }, nodes: [] },
91
- ] },
92
- ]
93
+ {
94
+ data: { num: " 4" , ref: null , custom: " abc" },
95
+ nodes: [{ data: { num: " 31" , ref: " 4" , custom: " 12" }, nodes: [] }],
96
+ },
97
+ {
98
+ data: { num: " 418" , ref: null , custom: " ü" },
99
+ nodes: [
100
+ { data: { num: " 1941" , ref: " 418" , custom: " de" }, nodes: [] },
101
+ { data: { num: " 1" , ref: " 418" , custom: " ZZZz" }, nodes: [] },
102
+ ],
103
+ },
104
+ ];
93
105
```
94
106
95
107
Example with no data field:
96
108
97
109
``` js
98
- const tree = arrayToTree ([
99
- { id: ' 4' , parentId: null , custom: ' abc' },
100
- { id: ' 31' , parentId: ' 4' , custom: ' 12' },
101
- { id: ' 1941' , parentId: ' 418' , custom: ' de' },
102
- { id: ' 1' , parentId: ' 418' , custom: ' ZZZz' },
103
- { id: ' 418' , parentId: null , custom: ' ü' },
104
- ], { dataField: null })
110
+ const tree = arrayToTree (
111
+ [
112
+ { id: " 4" , parentId: null , custom: " abc" },
113
+ { id: " 31" , parentId: " 4" , custom: " 12" },
114
+ { id: " 1941" , parentId: " 418" , custom: " de" },
115
+ { id: " 1" , parentId: " 418" , custom: " ZZZz" },
116
+ { id: " 418" , parentId: null , custom: " ü" },
117
+ ],
118
+ { dataField: null }
119
+ );
105
120
```
106
121
107
122
Which produces:
108
123
109
124
``` js
110
125
[
111
- { id: ' 4' , parentId: null , custom: ' abc' , children: [
112
- { id: ' 31' , parentId: ' 4' , custom: ' 12' , children: [] },
113
- ] },
114
- { id: ' 418' , parentId: null , custom: ' ü' , children: [
115
- { id: ' 1941' , parentId: ' 418' , custom: ' de' , children: [] },
116
- { id: ' 1' , parentId: ' 418' , custom: ' ZZZz' , children: [] },
117
- ] },
118
- ]
126
+ {
127
+ id: " 4" ,
128
+ parentId: null ,
129
+ custom: " abc" ,
130
+ children: [{ id: " 31" , parentId: " 4" , custom: " 12" , children: [] }],
131
+ },
132
+ {
133
+ id: " 418" ,
134
+ parentId: null ,
135
+ custom: " ü" ,
136
+ children: [
137
+ { id: " 1941" , parentId: " 418" , custom: " de" , children: [] },
138
+ { id: " 1" , parentId: " 418" , custom: " ZZZz" , children: [] },
139
+ ],
140
+ },
141
+ ];
119
142
```
120
143
121
144
Example with nested id/parentId properties:
122
145
123
146
``` js
124
- const tree = arrayToTree ([
125
- { num: { id: ' 4' }, parent: { parentId: null }, custom: ' abc' },
126
- { num: { id: ' 31' }, parent: { parentId: ' 4' }, custom: ' 12' },
127
- ], { id: ' num.id' , parentId: ' parent.parentId' })
147
+ const tree = arrayToTree (
148
+ [
149
+ { num: { id: " 4" }, parent: { parentId: null }, custom: " abc" },
150
+ { num: { id: " 31" }, parent: { parentId: " 4" }, custom: " 12" },
151
+ ],
152
+ { id: " num.id" , parentId: " parent.parentId" }
153
+ );
128
154
```
129
155
130
156
Which produces:
131
157
132
158
``` js
133
159
[
134
- { data: { num: { id: ' 4' }, parent: { parentId: null }, custom: ' abc' }, children: [
135
- { data: { num: { id: ' 31' }, parent: { parentId: ' 4' }, custom: ' 12' }, children: [] },
136
- ] },
137
- ]
160
+ {
161
+ data: { num: { id: " 4" }, parent: { parentId: null }, custom: " abc" },
162
+ children: [
163
+ {
164
+ data: { num: { id: " 31" }, parent: { parentId: " 4" }, custom: " 12" },
165
+ children: [],
166
+ },
167
+ ],
168
+ },
169
+ ];
138
170
```
139
171
140
172
## TypeScript
141
173
142
174
This project includes types, just import the module as usual:
143
175
144
176
``` ts
145
- import { arrayToTree } from ' performant-array-to-tree'
177
+ import { arrayToTree } from " performant-array-to-tree" ;
146
178
147
- const tree = arrayToTree (array )
179
+ const tree = arrayToTree (array );
148
180
```
0 commit comments