Array.prototype.flat()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.

Array 实例的 flat() 方法创建一个新数组,其中所有子数组元素递归地连接到该数组中,直至指定深度。

¥The flat() method of Array instances creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

Try it

语法

¥Syntax

js
flat()
flat(depth)

参数

¥Parameters

depth Optional

深度级别指定嵌套数组结构应展平的深度。默认为 1。

返回值

¥Return value

一个新数组,其中连接有子数组元素。

¥A new array with the sub-array elements concatenated into it.

描述

¥Description

flat() 方法是 复印法 方法。它不会更改 this,而是返回 浅拷贝,其中包含与原始数组中的元素相同的元素。

¥The flat() method is a copying method. It does not alter this but instead returns a shallow copy that contains the same elements as the ones from the original array.

如果要展平的数组是 sparse,则 flat() 方法会忽略空槽。例如,如果 depth 为 1,则根数组和第一层嵌套数组中的空槽都将被忽略,但进一步嵌套数组中的空槽将与数组本身一起保留。

¥The flat() method ignores empty slots if the array being flattened is sparse. For example, if depth is 1, both empty slots in the root array and in the first level of nested arrays are ignored, but empty slots in further nested arrays are preserved with the arrays themselves.

flat() 方法是 generic。它只期望 this 值具有 length 属性和整数键控属性。但是,如果要展平其元素,则它们必须是数组。

¥The flat() method is generic. It only expects the this value to have a length property and integer-keyed properties. However, its elements must be arrays if they are to be flattened.

示例

¥Examples

展平嵌套数组

¥Flattening nested arrays

js
const arr1 = [1, 2, [3, 4]];
arr1.flat();
// [1, 2, 3, 4]

const arr2 = [1, 2, [3, 4, [5, 6]]];
arr2.flat();
// [1, 2, 3, 4, [5, 6]]

const arr3 = [1, 2, [3, 4, [5, 6]]];
arr3.flat(2);
// [1, 2, 3, 4, 5, 6]

const arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
arr4.flat(Infinity);
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

在稀疏数组上使用 flat()

¥Using flat() on sparse arrays

flat() 方法删除数组中的空槽:

¥The flat() method removes empty slots in arrays:

js
const arr5 = [1, 2, , 4, 5];
console.log(arr5.flat()); // [1, 2, 4, 5]

const array = [1, , 3, ["a", , "c"]];
console.log(array.flat()); // [ 1, 3, "a", "c" ]

const array2 = [1, , 3, ["a", , ["d", , "e"]]];
console.log(array2.flat()); // [ 1, 3, "a", ["d", empty, "e"] ]
console.log(array2.flat(2)); // [ 1, 3, "a", "d", "e"]

对非数组对象调用 flat()

¥Calling flat() on non-array objects

flat() 方法读取 thislength 属性,然后访问键为小于 length 的非负整数的每个属性。如果元素不是数组,则直接附加到结果中。如果元素是数组,则根据 depth 参数对其进行展平。

¥The flat() method reads the length property of this and then accesses each property whose key is a nonnegative integer less than length. If the element is not an array, it's directly appended to the result. If the element is an array, it's flattened according to the depth parameter.

js
const arrayLike = {
  length: 3,
  0: [1, 2],
  // Array-like objects aren't flattened
  1: { length: 2, 0: 3, 1: 4 },
  2: 5,
  3: 3, // ignored by flat() since length is 3
};
console.log(Array.prototype.flat.call(arrayLike));
// [ 1, 2, { '0': 3, '1': 4, length: 2 }, 5 ]

规范

Specification
ECMAScript Language Specification
# sec-array.prototype.flat

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看