Function: mapTree()
ts
function mapTree<T, R>(
trees,
mapper,
options?): TreeNode<R>[];Maps over a tree structure, transforming each node while preserving the tree hierarchy. 遍历树结构,转换每个节点同时保持树的层次结构。
Type Parameters
| Type Parameter | Description |
|---|---|
T extends { [key: string]: any; } | The type of the original tree nodes / 原始树节点的类型 |
R extends { [key: string]: any; } | The type of the transformed tree nodes / 转换后树节点的类型 |
Parameters
| Parameter | Type | Description |
|---|---|---|
trees | TreeNode<T>[] | The tree structure to map over / 要遍历的树结构 |
mapper | (node, level, index) => R | Function to transform each node / 转换每个节点的函数 |
options | MapTreeOptions | Configuration options / 配置选项 |
Returns
TreeNode<R>[]
A new tree with transformed nodes / 转换后的新树
Example
ts
const tree = [{ id: 1, children: [{ id: 2 }] }]
mapTree(tree, (node) => ({ ...node, label: node.id }))
// => [{ id: 1, label: 1, children: [{ id: 2, label: 2 }] }]Since
1.2.0