---
title: "toList"
description: "将嵌套树形结构转换回包含父子关系字段的扁平数组，与 fromList 互为逆操作。"
seo_title: "toList"
seo_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/docs/transformers/tree/to-list"
---
# toList

> 将嵌套树形结构转换回包含父子关系字段的扁平数组，与 fromList 互为逆操作。

## 用法

`toList` 方法将树形结构转换回扁平数组。

```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 将会是:
[
  { 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[]`

### 参数

**tree** (`T[]`) *required*: 树形结构的节点数组。

**config** (`TreeConfig`): 用于自定义树形结构中 id, pid, children 键名的配置对象。可选。指定节点唯一标识符的键名。默认为 "id"。可选。指定节点父级标识符的键名。默认为 "pid"。可选。指定子节点数组的键名。默认为 "children"。

### 返回值

**T\[\]**: 返回一个扁平的节点数组。

## Changelog

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


## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
