Skip to content

Function: findTreeNode() ​

ts
function findTreeNode<T>(
   trees, 
   predicate, 
   childrenKey?): T | undefined;

Finds the first tree node that matches a predicate. 查找第一个满足断言条件的树节点。

Type Parameters ​

Type ParameterDescription
T extends Record<string, unknown>The node type / 节点类型

Parameters ​

ParameterTypeDescription
treesreadonly T[]The source tree array / 源树数组
predicate(node) => booleanThe match predicate / 匹配断言
childrenKeykeyof TThe 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

Released under the MIT License.