Function: listToTree()
ts
function listToTree<T>(items, options?): T[];Converts a flat list of items to a hierarchical tree structure. 将扁平的项目列表转换为分层树结构。
Type Parameters
| Type Parameter | Description |
|---|---|
T extends { [key: string]: any; } | The type of the items in the list / 列表中项目的类型 |
Parameters
| Parameter | Type | Description |
|---|---|---|
items | T[] | The flat list of items to convert / 要转换的扁平项目列表 |
options | ListToTreeOptions<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