77[ ![ Dependency Status] ( https://david-dm.org/philipstanislaus/performant-array-to-tree.svg )] ( https://david-dm.org/philipstanislaus/performant-array-to-tree )
88[ ![ 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 )
99[ ![ 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 )
1110[ ![ npm license] ( https://img.shields.io/npm/l/performant-array-to-tree.svg )] ( https://www.npmjs.com/package/performant-array-to-tree )
1211
1312Converts 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
3332
3433``` js
3534const 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+ ]);
4241```
4342
4443Which results in the following array:
4544
4645``` js
4746[
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+ ];
5661```
5762
5863## Configuration
@@ -69,80 +74,107 @@ You can provide a second argument to arrayToTree with configuration options. Rig
6974Example:
7075
7176``` 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+ );
7987```
8088
8189Which produces:
8290
8391``` js
8492[
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+ ];
93105```
94106
95107Example with no data field:
96108
97109``` 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+ );
105120```
106121
107122Which produces:
108123
109124``` js
110125[
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+ ];
119142```
120143
121144Example with nested id/parentId properties:
122145
123146``` 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+ );
128154```
129155
130156Which produces:
131157
132158``` js
133159[
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+ ];
138170```
139171
140172## TypeScript
141173
142174This project includes types, just import the module as usual:
143175
144176``` ts
145- import { arrayToTree } from ' performant-array-to-tree'
177+ import { arrayToTree } from " performant-array-to-tree" ;
146178
147- const tree = arrayToTree (array )
179+ const tree = arrayToTree (array );
148180```
0 commit comments