Skip to content

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

ParameterValue
T extends Object-
Key extends string"id"
ParentKey extends string"parentId"
ChildrenKey extends string"children"
DataKey extends stringnever
RemoveEmptyChildrenKey extends booleannever
MappingKey extends stringnever
MappingParentKey extends stringnever
MappingChildrenKey extends stringnever
Strict extends booleanfalse

Parameters

ParameterTypeDescription
arrayT[]包含父子级关系的数组列表
optionsToArrayTreeOptions<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

ts
// 默认树结构
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

ParameterValue
T extends ObjectObject
Key extends stringstring
ParentKey extends stringstring
ChildrenKey extends stringstring
DataKey extends stringnever
RemoveEmptyChildrenKey extends booleanfalse
MappingKey extends stringstring
MappingParentKey extends stringstring
MappingChildrenKey extends stringstring
Strict extends booleanfalse

Properties

PropertyTypeDescription
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 时当子节点为空将没有 childrenKeykeyMap.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 keyof T ? Key : never, ParentKey extends keyof T ? ParentKey : never, DataKey, MappingKey, MappingParentKey>, ChildrenKey, MappingChildrenKey, RemoveEmptyChildrenKey>

Type parameters

ParameterValue
T extends ObjectObject
Key extends stringstring
ParentKey extends stringstring
ChildrenKey extends stringstring
DataKey extends stringnever
RemoveEmptyChildrenKey extends booleanfalse
MappingKey extends stringnever
MappingParentKey extends stringnever
MappingChildrenKey extends stringnever