Skip to content

Function: treeDepth()

ts
function treeDepth<T>(trees, childrenKey?): number;

Calculates the maximum depth of a tree. 计算树结构的最大深度。

Type Parameters

Type ParameterDescription
T extends Record<string, unknown>The node type / 节点类型

Parameters

ParameterTypeDescription
treesreadonly T[]The source tree array / 源树数组
childrenKeykeyof TThe children field name / 子节点字段名

Returns

number

The maximum tree depth / 最大树深度

Example

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

Since

1.2.0

Released under the MIT License.