toList
Flatten a tree back into a flat array using the configured fields, the inverse of fromList for persistence and serialization.
Usage
The toList method converts a nested tree structure back into a flat array. It is the inverse operation of fromList.
example.ts
import { Tree } from '@movk/core'
const tree = [
{
id: 1,
pid: 0,
name: 'root',
children: [
{ id: 2, pid: 1, name: 'child 1' },
{ id: 3, pid: 1, name: 'child 2' }
]
}
]
const list = Tree.toList(tree)
/*
list will be:
[
{ id: 1, pid: 0, name: 'root' },
{ id: 2, pid: 1, name: 'child 1' },
{ id: 3, pid: 1, name: 'child 2' }
]
*/
API
toList<T extends TreeNode>(tree: T[], config?: TreeConfig): T[]
Parameters
tree
T[] required
The nested tree array of nodes.
config
TreeConfig
Configuration object for customizing the
id, pid, children key names in the tree structure.id
string
Optional. The key name for the node's unique identifier. Defaults to "id".
pid
string
Optional. The key name for the node's parent identifier. Defaults to "pid".
children
string
Optional. The key name for the children array. Defaults to "children".
Returns
T[]
Returns a flat array of nodes.
Changelog
No recent changes