Skip to content

Function: countTree() ​

ts
function countTree<T>(nodes, childrenKey?): number;

Counts all nodes in a tree (including roots). 统计树中的节点总数(含根节点)。

Type Parameters ​

Type ParameterDescription
T extends Record<string, unknown>Node type with optional children / 节点类型

Parameters ​

ParameterTypeDescription
nodesreadonly T[]Forest roots / 根节点数组
childrenKeykeyof T & stringChildren property name / children 字段名

Returns ​

number

Total node count / 节点总数

Example ​

ts
countTree([{ id: 1, children: [{ id: 2 }] }])
// => 2

Since ​

1.2.0

Released under the MIT License.