Skip to content

mapTree

Functions

mapTree()

mapTree<T, U, ChildrenKey, MappingChildrenKey>(array, iterator, options?): WithChildren<Omit<U, ChildrenKey | MappingChildrenKey>, ChildrenKey | MappingChildrenKey, false>[]

根据迭代器映射子节点生成新的树列表

Type parameters

ParameterValue
T extends Object-
U extends Object-
ChildrenKey extends string"children"
MappingChildrenKey extends stringnever

Parameters

ParameterTypeDescription
arrayT[]树列表
iteratorTreeIterator<T, U>迭代器
options?MapTreeOptions<ChildrenKey, MappingChildrenKey>配置项

Returns

WithChildren<Omit<U, ChildrenKey | MappingChildrenKey>, ChildrenKey | MappingChildrenKey, false>[]

Example

ts
mapTree(
  [
    {
      id: 1,
      text: '1',
      children: [
        {
          id: 3,
          text: '3',
        }
      ]
    },
    {
      id: 2,
      text: '2',
    }
  ],
  (node) => {
    if (node.id === 2) {
      node.text = '22'
    }
    return node
  },
)
// => [{...}, { id: 2, text: '22', }]

Interfaces

MapTreeOptions<ChildrenKey, MappingChildrenKey>

Type parameters

ParameterValue
ChildrenKey extends stringstring
MappingChildrenKey extends stringnever

Properties

PropertyTypeDescription
childrenKey?ChildrenKey子节点键

Default
'children'
mapChildrenKey?MappingChildrenKey映射后的子节点键,设置后则必定存在于节点上