Function: findTreeNode()
ts
function findTreeNode<T>(
trees,
predicate,
childrenKey?): T | undefined;Finds the first tree node that matches a predicate. 查找第一个满足断言条件的树节点。
Type Parameters
| Type Parameter | Description |
|---|---|
T extends Record<string, unknown> | The node type / 节点类型 |
Parameters
| Parameter | Type | Description |
|---|---|---|
trees | readonly T[] | The source tree array / 源树数组 |
predicate | (node) => boolean | The match predicate / 匹配断言 |
childrenKey | keyof T | The children field name / 子节点字段名 |
Returns
T | undefined
The first matching node, or undefined / 第一个匹配的节点,或 undefined
Example
ts
findTreeNode([{ id: 1, children: [{ id: 2 }] }], (node) => node.id === 2)
// => { id: 2 }Since
1.2.0