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.

Returns

T[]
Returns a flat array of nodes.

Changelog

No recent changes
Copyright © 2024 - 2026 YiXuan - MIT License