Function: filterTree()
ts
function filterTree<T>(
trees,
predicate,
childrenKey?): T[];Filters a tree while preserving the ancestor chain of matched nodes. 过滤树结构,并保留匹配节点的祖先链。
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[]
A filtered tree / 过滤后的树
Example
ts
filterTree([{ id: 1, visible: true }], (node) => Boolean(node.visible))
// => [{ id: 1, visible: true }]Since
1.2.0