|
1 |
| -# tree2array |
2 |
| - |
3 |
| -tree to array and generate id, parentId and leaf when undefined |
4 |
| - |
5 |
| -## 调用 |
| 1 | +# 树转列表 |
6 | 2 |
|
7 | 3 | ```js
|
8 | 4 | tree2array(tree, [options])
|
9 | 5 | ```
|
10 | 6 |
|
| 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 | + |
11 | 20 | ## 返回
|
12 | 21 |
|
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