Skip to content

Function: listToTree() ​

ts
function listToTree<T>(items, options?): T[];

Converts a flat list of items to a hierarchical tree structure. 将扁平的项目列表转换为分层树结构。

Type Parameters ​

Type ParameterDescription
T extends { [key: string]: any; }The type of the items in the list / 列表中项目的类型

Parameters ​

ParameterTypeDescription
itemsT[]The flat list of items to convert / 要转换的扁平项目列表
optionsListToTreeOptions<T>Configuration options for the conversion / 转换的配置选项

Returns ​

T[]

The hierarchical tree structure / 分层树结构

Example ​

ts
const list = [{ id: 1, parentId: null }, { id: 2, parentId: 1 }]
listToTree(list)
// => [{ id: 1, parentId: null, children: [{ id: 2, parentId: 1, children: [] }] }]

Since ​

1.2.0

Released under the MIT License.