---
title: "flatten"
description: "数组扁平化，将嵌套数组展平到指定深度"
seo_title: "flatten"
seo_description: "Flatten a nested array to a given depth, returning a new flattened array while leaving the original nested structure untouched."
canonical_url: "https://core.mhaibaraai.cn/docs/utilities/array/flatten"
---
# flatten

> 数组扁平化，将嵌套数组展平到指定深度

## 用法

`flatten` 函数用于将嵌套数组展平到指定深度。

```ts
import { flatten } from '@movk/core'

const nested = [1, [2, 3], [4, [5, 6]]]
const flat1 = flatten(nested)
console.log(flat1) // [1, 2, 3, 4, [5, 6]]

const flat2 = flatten(nested, 2)
console.log(flat2) // [1, 2, 3, 4, 5, 6]
```

## API

### `flatten<T>(arr, depth?)`

将嵌套数组展平到指定深度。

### 参数

**arr** (`T[]`) *required*: 待扁平化的数组。

**depth** (`number`): 扁平化深度，默认为 1。

### 返回值

**返回值** (`any[]`): 扁平化后的数组。

## Changelog

See commit history for [src/utilities/array/flatten.ts](https://github.com/mhaibaraai/movk-core/commits/main/src/utilities/array/flatten.ts).


## Sitemap

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