conversion-library / arrayToTree
Function: arrayToTree()
ts
function arrayToTree<T, IdK, parentIdK>(array: T[], options: ArrayToTreeOptionsType<IdK, parentIdK>): ArrayToTreeReturnItemType<T>[]
arrayToTree
version: v0.0.14+Type Parameters
• T
• IdK extends string
| number
| symbol
• parentIdK extends string
| number
| symbol
Parameters
• array: T
[]
输入数组
• options: ArrayToTreeOptionsType
<IdK
, parentIdK
> = {}
参数
idKey
- id key, defaultid
parentIdKey
- parent id key, defaultparentId
Returns
ArrayToTreeReturnItemType
<T
>[]
返回树形结构
Example
ts
const array = [
{ id: 1, parentId: null, name: 'aaa' },
{ id: 2, parentId: 1, name: 'bbb' },
{ id: 3, parentId: 1, name: 'ccc' },
{ id: 4, parentId: 2, name: 'ddd' },
]
console.log(arrayToTree(array))
// [
// { id: 1, parentId: null, name: 'aaa', children: [
// { id: 2, parentId: 1, name: 'bbb', children: [
// { id: 4, parentId: 2, name: 'ddd' }
// ]},
// { id: 3, parentId: 1, name: 'ccc' }
// ]}
// ]