Skip to content

Function: filterTree()

ts
function filterTree<T>(
   trees, 
   predicate, 
   childrenKey?): T[];

Filters a tree while preserving the ancestor chain of matched nodes. 过滤树结构,并保留匹配节点的祖先链。

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[]

A filtered tree / 过滤后的树

Example

ts
filterTree([{ id: 1, visible: true }], (node) => Boolean(node.visible))
// => [{ id: 1, visible: true }]

Since

1.2.0

Released under the MIT License.