Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | 1x 5x 5x 5x 5x 5x 2x 3x 4x 4x 2x | /** * @description 通过自定义函数cb,格式化tree为所需,直接在cb函数改变数值即可 * @function formatTree * @param {object} obj - 入参对象 * @param {object} obj.tree - 原始tree * @param {string} obj.key - 子树的属性名称 * @param {function} [obj.cb] - 自定义格式化函数 * @returns {array} 通过自定义格式化函数,格式化后的tree * @author liukun <919590347@qq.com> * @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 */ type Fn = (...rest: any[]) => boolean | void interface Param { tree: Record<string, any> cb: Fn level?: number key?: string } export function formatTree(param: Param): boolean | void { const { tree, cb, level = 1, key = 'children' } = param const treeItem = tree[key] const editedObj = cb(tree, level) Iif (editedObj) return editedObj if (!(Array.isArray(treeItem) && treeItem.length > 0)) { return true } for (const item of treeItem) { const needStop = formatTree({ tree: item, cb, key, level: level + 1, }) if (needStop) { break } } } |