Skip to content

Function: withVirtualRoot()

ts
function withVirtualRoot<T>(data, config?): T[];

为扁平树形数据添加虚拟根节点,并将所有顶级节点(parentId 为 null/undefined)挂到虚拟根下。

Type Parameters

Type Parameter
T extends Record<string, any>

Parameters

ParameterType
dataT[]
configVirtualRootConfig

Returns

T[]

Example

ts
const result = withVirtualRoot(
  [{ id: '1', name: 'A', parentId: null }],
  { idField: 'id', idValue: VIRTUAL_ROOT_ID, nameField: 'name', nameValue: 'Root', parentIdField: 'parentId' }
)
result // => [{ id: '00000000-...', name: 'Root', children: [{ id: '1', name: 'A', ... }] }]

Since

1.0.0

Released under the MIT License.