Skip to content

Commit 59b08fc

Browse files
UladUlad
authored andcommitted
Add nodesField option
1 parent 87f9c14 commit 59b08fc

9 files changed

+37
-33
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ You can provide a second argument to arrayToTree with configuration options. Rig
6262
- `id`: key of the id field of the item. Default: `"id"`
6363
- `parentId`: key of the parent's id field of the item. Default: `"parentId"`
6464
- `dataField`: key which will contain all properties/data of the original items. Set to null if you don't want a container. Default: `"data"`
65+
- `nodesField`: key which will contain all child nodes of the parent node . Default: `"children"`
6566

6667
Example:
6768

@@ -72,19 +73,19 @@ const tree = arrayToTree([
7273
{ num: '1941', ref: '418', custom: 'de' },
7374
{ num: '1', ref: '418', custom: 'ZZZz' },
7475
{ num: '418', ref: null, custom: 'ü'},
75-
], { id: 'num', parentId: 'ref' })
76+
], { id: 'num', parentId: 'ref', nodesField: 'nodes' })
7677
```
7778

7879
Which produces:
7980

8081
```js
8182
[
82-
{ data: { num: '4', ref: null, custom: 'abc' }, children: [
83-
{ data: { num: '31', ref: '4', custom: '12' }, children: [] },
83+
{ data: { num: '4', ref: null, custom: 'abc' }, nodes: [
84+
{ data: { num: '31', ref: '4', custom: '12' }, nodes: [] },
8485
] },
85-
{ data: { num: '418', ref: null, custom: 'ü'}, children: [
86-
{ data: { num: '1941', ref: '418', custom: 'de' }, children: [] },
87-
{ data: { num: '1', ref: '418', custom: 'ZZZz' }, children: [] },
86+
{ data: { num: '418', ref: null, custom: 'ü'}, nodes: [
87+
{ data: { num: '1941', ref: '418', custom: 'de' }, nodes: [] },
88+
{ data: { num: '1', ref: '418', custom: 'ZZZz' }, nodes: [] },
8889
] },
8990
]
9091
```

build/arrayToTree.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ export interface Item {
66
export interface TreeItem {
77
id?: string;
88
parentId?: string | null;
9-
[key: string]: Item | any;
10-
children: TreeItem[];
9+
[key: string]: Item | TreeItem[] | any;
1110
}
1211
export interface Config {
1312
id: string;
1413
parentId: string;
1514
dataField: string | null;
15+
nodesField: string;
1616
}
1717
/**
1818
* Unflattens an array to a tree with runtime O(n)

build/arrayToTree.js

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/arrayToTree.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/arrayToTree.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/arrayToTree.spec.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)