Skip to content

Commit 1ec9acf

Browse files
committed
docs: update tree2array
1 parent d24216b commit 1ec9acf

File tree

3 files changed

+235
-110
lines changed

3 files changed

+235
-110
lines changed

docs/array2tree.md

Lines changed: 49 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -69,69 +69,56 @@ array2tree(cloneDeep(array)) // tree
6969
```
7070

7171
```json [结果]
72-
[
73-
{
74-
"id": "chart",
75-
"path": "/chart",
76-
"parentId": null,
77-
"deep": 0,
78-
"children": [
79-
{
80-
"id": "chartIndex",
81-
"path": "/chart/index",
82-
"parentId": "chart",
83-
"deep": 1,
84-
"children": [
85-
{
86-
"id": "chartIndexActive",
87-
"path": "/chart/index/active",
88-
"parentId": "chartIndex",
89-
"deep": 2,
90-
"children": [
91-
{
92-
"id": "chartIndexActiveMy",
93-
"path": "/chart/index/active/my",
94-
"parentId": "chartIndexActive",
95-
"deep": 3,
96-
"leaf": true
97-
}
98-
]
99-
}
100-
]
101-
},
102-
{
103-
"id": "chartReview",
104-
"path": "/chart/review",
105-
"parentId": "chart",
106-
"deep": 1,
72+
[{
73+
"id": "chart",
74+
"path": "/chart",
75+
"parentId": null,
76+
"deep": 0,
77+
"children": [{
78+
"id": "chartIndex",
79+
"path": "/chart/index",
80+
"parentId": "chart",
81+
"deep": 1,
82+
"children": [{
83+
"id": "chartIndexActive",
84+
"path": "/chart/index/active",
85+
"parentId": "chartIndex",
86+
"deep": 2,
87+
"children": [{
88+
"id": "chartIndexActiveMy",
89+
"path": "/chart/index/active/my",
90+
"parentId": "chartIndexActive",
91+
"deep": 3,
10792
"leaf": true
108-
},
109-
{
110-
"id": "chartProject",
111-
"path": "/chart/project",
112-
"parentId": "chart",
113-
"deep": 1,
114-
"leaf": true
115-
}
116-
]
117-
},
118-
{
119-
"id": "smile",
120-
"path": "/smile",
121-
"parentId": null,
122-
"deep": 0,
123-
"children": [
124-
{
125-
"id": "smileIndex",
126-
"path": "/smile/index",
127-
"test": true,
128-
"parentId": "smile",
129-
"deep": 1,
130-
"leaf": true
131-
}
132-
]
133-
}
134-
]
93+
}]
94+
}]
95+
}, {
96+
"id": "chartReview",
97+
"path": "/chart/review",
98+
"parentId": "chart",
99+
"deep": 1,
100+
"leaf": true
101+
}, {
102+
"id": "chartProject",
103+
"path": "/chart/project",
104+
"parentId": "chart",
105+
"deep": 1,
106+
"leaf": true
107+
}]
108+
}, {
109+
"id": "smile",
110+
"path": "/smile",
111+
"parentId": null,
112+
"deep": 0,
113+
"children": [{
114+
"id": "smileIndex",
115+
"path": "/smile/index",
116+
"test": true,
117+
"parentId": "smile",
118+
"deep": 1,
119+
"leaf": true
120+
}]
121+
}]
135122
```
136123
:::
137124

docs/param.md

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
| 名称 | 说明 |
66
| ------- | ------------ |
77
| options | 配置选项 |
8-
| tree | 树状结构数据 |
98
| array | 列表结构数据 |
9+
| tree | 树状结构数据 |
1010

1111
## options
1212

@@ -34,6 +34,47 @@
3434
}
3535
```
3636

37+
## array
38+
39+
列表数据结构,示例如下:
40+
41+
```json
42+
[{
43+
"id": "chart",
44+
"path": "/chart",
45+
"parentId": null
46+
}, {
47+
"id": "chartIndex",
48+
"path": "/chart/index",
49+
"parentId": "chart"
50+
}, {
51+
"id": "chartIndexActive",
52+
"path": "/chart/index/active",
53+
"parentId": "chartIndex"
54+
}, {
55+
"id": "chartIndexActiveMy",
56+
"path": "/chart/index/active/my",
57+
"parentId": "chartIndexActive"
58+
}, {
59+
"id": "chartReview",
60+
"path": "/chart/review",
61+
"parentId": "chart"
62+
}, {
63+
"id": "chartProject",
64+
"path": "/chart/project",
65+
"parentId": "chart"
66+
}, {
67+
"id": "smile",
68+
"path": "/smile",
69+
"parentId": null
70+
}, {
71+
"id": "smileIndex",
72+
"path": "/smile/index",
73+
"test": true,
74+
"parentId": "smile"
75+
}]
76+
```
77+
3778
## tree
3879

3980
树状结构数据,示例如下:
@@ -78,44 +119,3 @@
78119
}]
79120
}]
80121
```
81-
82-
## array
83-
84-
列表数据结构,示例如下:
85-
86-
```json
87-
[{
88-
"id": "chart",
89-
"path": "/chart",
90-
"parentId": null
91-
}, {
92-
"id": "chartIndex",
93-
"path": "/chart/index",
94-
"parentId": "chart"
95-
}, {
96-
"id": "chartIndexActive",
97-
"path": "/chart/index/active",
98-
"parentId": "chartIndex"
99-
}, {
100-
"id": "chartIndexActiveMy",
101-
"path": "/chart/index/active/my",
102-
"parentId": "chartIndexActive"
103-
}, {
104-
"id": "chartReview",
105-
"path": "/chart/review",
106-
"parentId": "chart"
107-
}, {
108-
"id": "chartProject",
109-
"path": "/chart/project",
110-
"parentId": "chart"
111-
}, {
112-
"id": "smile",
113-
"path": "/smile",
114-
"parentId": null
115-
}, {
116-
"id": "smileIndex",
117-
"path": "/smile/index",
118-
"test": true,
119-
"parentId": "smile"
120-
}]
121-
```

docs/tree2array.md

Lines changed: 144 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,151 @@
1-
# tree2array
2-
3-
tree to array and generate id, parentId and leaf when undefined
4-
5-
## 调用
1+
# 树转列表
62

73
```js
84
tree2array(tree, [options])
95
```
106

7+
树状结构数据转换为列表结构数据,并在未定义主键(`id`)、父键(`parentId`)和叶子(`leaf`)时**自动生成**它们。
8+
9+
::: tip 深拷贝
10+
推荐使用 [lodash.clonedeep] 深拷贝,以避免原始数据遭到不必要的篡改。
11+
:::
12+
13+
## 参数
14+
15+
| 参数 | 类型 | 默认 | 说明 |
16+
| --------- | -------- | ---- | ------------------------------- |
17+
| `tree` | `array` | | [树状结构数据](./param.md#tree) |
18+
| `options` | `object` | | [配置选项](./param.md#options) |
19+
1120
## 返回
1221

13-
array
22+
| 参数 | 类型 | 说明 |
23+
| ---- | ------- | -------------------------------- |
24+
| * | `array` | [列表结构数据](./param.md#array) |
25+
26+
## 示例
27+
28+
::: code-group
29+
```js [调用]
30+
import cloneDeep from 'lodash.clonedeep'
31+
import { tree2array } from '@axolo/tree-array'
32+
33+
const tree = [{
34+
path: '/home'
35+
}, {
36+
path: '/chart',
37+
children: [{
38+
path: '/chart/index',
39+
children: [{
40+
path: '/chart/index/top'
41+
}, {
42+
path: '/chart/index/active',
43+
children: [{
44+
path: '/chart/index/active/my'
45+
}]
46+
}]
47+
}, {
48+
path: '/chart/review'
49+
}, {
50+
path: '/chart/project',
51+
children: [{
52+
path: '/chart/project/you',
53+
test: true
54+
}, {
55+
path: '/chart/project/my',
56+
children: [{
57+
path: '/chart/project/my/one'
58+
}, {
59+
path: '/chart/project/my/two'
60+
}]
61+
}]
62+
}]
63+
}, {
64+
path: '/smile',
65+
children: [{
66+
path: '/smile/index',
67+
test: true
68+
}]
69+
}]
70+
71+
tree2array(cloneDeep(tree)) // array
72+
```
73+
74+
```json [结果]
75+
[{
76+
"path": "/home",
77+
"parentId": null,
78+
"id": "5tnchk22z3w",
79+
"leaf": true
80+
}, {
81+
"path": "/chart",
82+
"parentId": null,
83+
"id": "oabyvsti24",
84+
"leaf": false
85+
}, {
86+
"path": "/chart/index",
87+
"parentId": "oabyvsti24",
88+
"id": "r5s1atye4p",
89+
"leaf": false
90+
}, {
91+
"path": "/chart/index/top",
92+
"parentId": "r5s1atye4p",
93+
"id": "0tw2dgclqao",
94+
"leaf": true
95+
}, {
96+
"path": "/chart/index/active",
97+
"parentId": "r5s1atye4p",
98+
"id": "iji2k8j9rj",
99+
"leaf": false
100+
}, {
101+
"path": "/chart/index/active/my",
102+
"parentId": "iji2k8j9rj",
103+
"id": "u11xvewau8",
104+
"leaf": true
105+
}, {
106+
"path": "/chart/review",
107+
"parentId": "oabyvsti24",
108+
"id": "y9b2oungnke",
109+
"leaf": true
110+
}, {
111+
"path": "/chart/project",
112+
"parentId": "oabyvsti24",
113+
"id": "l5mjmml40fh",
114+
"leaf": false
115+
}, {
116+
"path": "/chart/project/you",
117+
"test": true,
118+
"parentId": "l5mjmml40fh",
119+
"id": "l6boi1z3uk",
120+
"leaf": true
121+
}, {
122+
"path": "/chart/project/my",
123+
"parentId": "l5mjmml40fh",
124+
"id": "ch00zi2xlbr",
125+
"leaf": false
126+
}, {
127+
"path": "/chart/project/my/one",
128+
"parentId": "ch00zi2xlbr",
129+
"id": "h1x7bwx2cew",
130+
"leaf": true
131+
}, {
132+
"path": "/chart/project/my/two",
133+
"parentId": "ch00zi2xlbr",
134+
"id": "hthwywtm8ac",
135+
"leaf": true
136+
}, {
137+
"path": "/smile",
138+
"parentId": null,
139+
"id": "jb5jfr58glj",
140+
"leaf": false
141+
}, {
142+
"path": "/smile/index",
143+
"test": true,
144+
"parentId": "jb5jfr58glj",
145+
"id": "ab0yw0uxl7n",
146+
"leaf": true
147+
}]
148+
```
149+
:::
150+
151+
[lodash.clonedeep]: https://www.npmjs.com/package/lodash.clonedeep

0 commit comments

Comments
 (0)