---
title: "toList"
description: "Flatten a tree back into a flat array using the configured fields, the inverse of fromList for persistence and serialization."
canonical_url: "https://core.mhaibaraai.cn/en/docs/transformers/tree/to-list"
---
# 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`.

```ts [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.Optional. The key name for the node's unique identifier. Defaults to "id".Optional. The key name for the node's parent identifier. Defaults to "pid".Optional. The key name for the children array. Defaults to "children".

### Returns

**T[]**: Returns a flat array of nodes.

## Changelog

See commit history for [src/transformers/tree/convert.ts](https://github.com/mhaibaraai/movk-core/commits/main/src/transformers/tree/convert.ts).

---

- [GitHub](https://github.com/mhaibaraai/movk-core/blob/main/src/transformers/tree/convert.ts)


## Sitemap

See the full [sitemap](https://core.mhaibaraai.cn/sitemap.md) for all pages.
