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 Parameter | Description |
|---|---|
T extends { [key: string]: any; } | The type of the tree nodes / 树节点的类型 |
Parameters
| Parameter | Type | Description |
|---|---|---|
trees | TreeNode<T>[] | The tree structure to walk through / 要遍历的树结构 |
visitor | (node, level, index) => boolean | void | Function called for each node / 为每个节点调用的函数 |
options | WalkTreeOptions | Configuration 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