# formatTree(obj) ⇒ array
通过自定义函数cb,格式化tree为所需,直接在cb函数改变数值即可
Returns: array
-
通过自定义格式化函数,格式化后的tree
Author: liukun 919590347@qq.com
Param | Type | Description |
---|---|---|
obj | object | 入参对象 |
obj.tree | object | 原始tree |
obj.key | string | 子树的属性名称 |
[obj.cb] | function | 自定义格式化函数 |
Example
import { formatTree } from '@wont/utils'
const beforeTree = {
id: 0,
children: [
{
id: 1,
label: '1-1',
children: [
{
id: 11,
label: '1-1-1',
children: [
{
id: 111,
label: '1-1-1-1',
},
],
},
],
},
{
id: 2,
label: '2-1',
},
],
}
const afterTree = {
id: 0,
value: 0,
selectable: false,
children: [
{
id: 1,
value: 1,
label: '1-1',
selectable: false,
children: [
{
id: 11,
value: 11,
label: '1-1-1',
selectable: false,
children: [
{
id: 111,
value: 111,
label: '1-1-1-1',
},
],
},
],
},
{
id: 2,
value: 2,
label: '2-1',
},
],
}
formatTree({
tree: beforeTree,
key: 'children',
cb: (obj) => {
const result = obj
result.value = obj.id
if (result.children?.length > 0) {
result.selectable = false
}
},
})
// returns afterTree