flatten

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

用法

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

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

7c832 — refactor!: 重构模块架构,拆分 utils 为专业化模块

Copyright © 2024 - 2026 YiXuan - MIT License