Skip to content

searchTree

Functions

searchTree()

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

根据迭代器搜索树列表的子项数据,区别于 filterTree 会返回完整的树形结构列表

注意:当父级符合搜索规则时将保留所有子级数据!

Type parameters

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

Parameters

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

Returns

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

Example

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

Interfaces

SearchTreeOptions<ChildrenKey, MappingChildrenKey>

Type parameters

ParameterValue
ChildrenKey extends stringstring
MappingChildrenKey extends stringnever

Properties

PropertyTypeDescription
childrenKey?ChildrenKey子节点键

Default
'children'
mapChildrenKey?MappingChildrenKey映射后的子节点键,设置后则必定存在于节点上
original?boolean是否返回源对象引用,易改变源对象数据结构

Default
false
originalDataKey?string源数据引用键,originalfalse 时生效,未设置时则不引用源数据