flit-kit
指南
API
NPM
指南
API
NPM
  • 总览
  • Tree

    • buildTree
    • traverseTree
    • findNode
    • getNodePath
    • normalizeChildren
    • pruneTreeDepth
    • getDescendants
    • getTreeDepth
    • flattenTree
    • renameTreeKeys
  • Number

    • animation

renameTreeKeys Tree

作用

按映射规则重写树节点属性名,支持专门重命名 children 字段。

签名

renameTreeKeys({ tree, mapping={}, childrenKey='children', childrenKeyOut })

引入

import { renameTreeKeys } from 'flit-kit'

参数

参数类型默认值说明
treeArray<object>[]输入树
mappingRecord<string,string>{}普通属性名映射,如 { id:'code' }
childrenKeystring'children'输入树的子节点字段名
childrenKeyOutstringundefined输出树的子节点字段名,如 'childrenList'

示例

const src = [
  { id: 1, name: 'A', children: [ { id: 2, name: 'A-1' } ] }
]
const out = renameTreeKeys({
  tree: src,
  mapping: { id: 'code', name: 'label' },
  childrenKey: 'children',
  childrenKeyOut: 'childrenList',
})

复杂度

  • O(n·k)

返回值

  • Array<object>

输出

[
  {
    "code": 1,
    "label": "A",
    "childrenList": [
      { "code": 2, "label": "A-1" }
    ]
  }
]
Prev
flattenTree