Skip to content

Commit 3b13e7d

Browse files
author
Philipp Alferov
committed
Refine readme
1 parent 41409ff commit 3b13e7d

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

readme.md

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ Solves a problem with conversion of retrieved from a database sets of data to a
1010
$ npm install --save array-to-tree
1111
```
1212

13-
## Basic Usage
13+
## Usage
14+
15+
### Basic
1416

1517
```js
1618
var arrayToTree = require('array-to-tree');
1719

18-
var navigation = [{
20+
var data = [{
1921
id: 1,
2022
name: "Portfolio",
2123
parent_id: null
@@ -33,6 +35,8 @@ var navigation = [{
3335
parent_id: null
3436
}];
3537

38+
var tree = arrayToTree({ data: data });
39+
3640
/*
3741
* Output:
3842
* Portfolio
@@ -41,10 +45,47 @@ var navigation = [{
4145
* About Me
4246
*/
4347

44-
var navigationTree = arrayToTree({ data: navigation });
4548
```
4649

47-
## Documentation
50+
### With Custom Attributes
51+
52+
```js
53+
var arrayToTree = require('array-to-tree');
54+
55+
var data = [{
56+
_id: 'ec654ec1-7f8f-11e3-ae96-b385f4bc450c',
57+
name: "Portfolio",
58+
parent: null
59+
}, {
60+
_id: 'ec666030-7f8f-11e3-ae96-0123456789ab',
61+
name: "Web Development",
62+
parent: 'ec654ec1-7f8f-11e3-ae96-b385f4bc450c'
63+
}, {
64+
_id: 'ec66fc70-7f8f-11e3-ae96-000000000000',
65+
name: "Recent Works",
66+
parent: 'ec666030-7f8f-11e3-ae96-0123456789ab'
67+
}, {
68+
_id: '32a4fbed-676d-47f9-a321-cb2f267e2918',
69+
name: "About Me",
70+
parent: null
71+
}];
72+
73+
var tree = arrayToTree({
74+
parentProperty: 'parent',
75+
customID: '_id'
76+
data: data
77+
});
78+
79+
/*
80+
* Output:
81+
* Portfolio
82+
* Web Development
83+
* Recent Works
84+
* About Me
85+
*/
86+
```
87+
88+
## API
4889

4990
### `arrayToTree(options)`
5091
Convert a plain array of nodes (with pointers to parent nodes) to a tree.

0 commit comments

Comments
 (0)