Skip to content

Function: walkTree()

ts
function walkTree<T>(
   trees, 
   visitor, 
   options?): boolean;

Walks through a tree structure, calling a visitor function for each node. 遍历树结构,为每个节点调用访问者函数。

Type Parameters

Type ParameterDescription
T extends { [key: string]: any; }The type of the tree nodes / 树节点的类型

Parameters

ParameterTypeDescription
treesTreeNode<T>[]The tree structure to walk through / 要遍历的树结构
visitor(node, level, index) => boolean | voidFunction called for each node / 为每个节点调用的函数
optionsWalkTreeOptionsConfiguration options / 配置选项

Returns

boolean

Whether the traversal completed successfully / 遍历是否成功完成

Example

typescript
const ids = []
walkTree([{ id: 1, children: [{ id: 2 }] }], (node) => { ids.push(node.id) })
ids // => [2, 1]

Since

1.2.0

Released under the MIT License.