Function: countTree()
ts
function countTree<T>(nodes, childrenKey?): number;Counts all nodes in a tree (including roots). 统计树中的节点总数(含根节点)。
Type Parameters
| Type Parameter | Description |
|---|---|
T extends Record<string, unknown> | Node type with optional children / 节点类型 |
Parameters
| Parameter | Type | Description |
|---|---|---|
nodes | readonly T[] | Forest roots / 根节点数组 |
childrenKey | keyof T & string | Children property name / children 字段名 |
Returns
number
Total node count / 节点总数
Example
ts
countTree([{ id: 1, children: [{ id: 2 }] }])
// => 2Since
1.2.0