toArrayTree
Functions
toArrayTree()
toArrayTree<
T
,Key
,ParentKey
,ChildrenKey
,DataKey
,RemoveEmptyChildrenKey
,MappingKey
,MappingParentKey
,MappingChildrenKey
,Strict
>(array
,options
):TreeNode
<T
,Key
,ParentKey
,ReplaceNever
<ReplaceEmpty
<ChildrenKey
,never
>,"children"
>,ReplaceEmpty
<DataKey
,never
>,ReplaceNever
<RemoveEmptyChildrenKey
,Strict
>,MappingKey
,MappingParentKey
,MappingChildrenKey
>[]
将一个层级关系的数据列表转成树结构列表
Type parameters
Parameter | Value |
---|---|
T extends Object | - |
Key extends string | "id" |
ParentKey extends string | "parentId" |
ChildrenKey extends string | "children" |
DataKey extends string | never |
RemoveEmptyChildrenKey extends boolean | never |
MappingKey extends string | never |
MappingParentKey extends string | never |
MappingChildrenKey extends string | never |
Strict extends boolean | false |
Parameters
Parameter | Type | Description |
---|---|---|
array | T [] | 包含父子级关系的数组列表 |
options | ToArrayTreeOptions <T , Key , ParentKey , ChildrenKey , DataKey , ReplaceNever <RemoveEmptyChildrenKey , Strict >, MappingKey , MappingParentKey , MappingChildrenKey , Strict > | 配置项 |
Returns
TreeNode
<T
, Key
, ParentKey
, ReplaceNever
<ReplaceEmpty
<ChildrenKey
, never
>, "children"
>, ReplaceEmpty
<DataKey
, never
>, ReplaceNever
<RemoveEmptyChildrenKey
, Strict
>, MappingKey
, MappingParentKey
, MappingChildrenKey
>[]
Example
// 默认树结构
const list = [
{ id: 1, name: '111' },
{ id: 2, parentId: 1, name: '222' },
{ id: 3, name: '333' },
{ id: 4, parentId: 2, name: '444' }
]
const tree = toArrayTree(list)
[
{
id: 1,
name: '111',
children: [
{
id: 2,
name: '222',
parentId: 1,
children: [
{
id: 4,
name: '444',
parentId: 2,
children: []
}
]
}
]
},
{
id: 3,
name: '333',
children: []
}
]
Interfaces
ToArrayTreeOptions<T, Key, ParentKey, ChildrenKey, DataKey, RemoveEmptyChildrenKey, MappingKey, MappingParentKey, MappingChildrenKey, Strict>
Type parameters
Parameter | Value |
---|---|
T extends Object | Object |
Key extends string | string |
ParentKey extends string | string |
ChildrenKey extends string | string |
DataKey extends string | never |
RemoveEmptyChildrenKey extends boolean | false |
MappingKey extends string | string |
MappingParentKey extends string | string |
MappingChildrenKey extends string | string |
Strict extends boolean | false |
Properties
Property | Type | Description |
---|---|---|
childrenKey? | ChildrenKey | 子节点键 Default 'children' |
dataKey? | DataKey | 数据存放键,未设置时将平铺属性 |
key? | Key | 节点键 Default 'id' |
keyMap? | Object | 键映射,映射后节点将必定包含映射键 |
keyMap.childrenKey? | MappingChildrenKey | - |
keyMap.key? | MappingKey | - |
keyMap.parentKey? | MappingParentKey | - |
orderBy? | _OrderByParams <T > | 排序数组,依赖于 orderBy() |
parentKey? | ParentKey | 父节点键 Default 'parentId' |
removeEmptyChildrenKey? | MaybeFn <RemoveEmptyChildrenKey , [TreeNode <T , Key , ParentKey , ChildrenKey , DataKey , false , MappingKey , MappingParentKey , MappingChildrenKey >]> | 是否移除空子节点属性,设为 true 时当子节点为空将没有 childrenKey 和 keyMap.childrenKey 属性Default options.strict |
strict? | Strict | 严格模式,如果设为 true ,会去掉父子关联不存在数据Default false |
Type Aliases
TreeNode<T, Key, ParentKey, ChildrenKey, DataKey, RemoveEmptyChildrenKey, MappingKey, MappingParentKey, MappingChildrenKey>
TreeNode<
T
,Key
,ParentKey
,ChildrenKey
,DataKey
,RemoveEmptyChildrenKey
,MappingKey
,MappingParentKey
,MappingChildrenKey
>:TreeNodeWithChildren
<TreeNodeBase
<T
,Key
extends keyofT
?Key
:never
,ParentKey
extends keyofT
?ParentKey
:never
,DataKey
,MappingKey
,MappingParentKey
>,ChildrenKey
,MappingChildrenKey
,RemoveEmptyChildrenKey
>
Type parameters
Parameter | Value |
---|---|
T extends Object | Object |
Key extends string | string |
ParentKey extends string | string |
ChildrenKey extends string | string |
DataKey extends string | never |
RemoveEmptyChildrenKey extends boolean | false |
MappingKey extends string | never |
MappingParentKey extends string | never |
MappingChildrenKey extends string | never |