Array.prototype.concat()

Array 实例的 concat() 方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。

¥The concat() method of Array instances is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

Try it

语法

¥Syntax

js
concat()
concat(value1)
concat(value1, value2)
concat(value1, value2, /* …, */ valueN)

参数

¥Parameters

value1, …, valueN Optional

要连接到新数组中的数组和/或值。如果省略所有 valueN 参数,则 concat 返回调用它的现有数组的 浅拷贝。请参阅下面的描述以了解更多详细信息。

返回值

¥Return value

一个新的 Array 实例。

¥A new Array instance.

描述

¥Description

concat 方法创建一个新数组。该数组将首先由调用它的对象中的元素填充。然后,对于每个参数,其值将连接到数组中 - 对于普通对象或基元,参数本身将成为最终数组的元素;对于属性 Symbol.isConcatSpreadable 设置为真值的数组或类数组对象,参数的每个元素将独立添加到最终数组中。concat 方法不会递归到嵌套数组参数中。

¥The concat method creates a new array. The array will first be populated by the elements in the object on which it is called. Then, for each argument, its value will be concatenated into the array — for normal objects or primitives, the argument itself will become an element of the final array; for arrays or array-like objects with the property Symbol.isConcatSpreadable set to a truthy value, each element of the argument will be independently added to the final array. The concat method does not recurse into nested array arguments.

concat() 方法是 复印法 方法。它不会更改 this 或作为参数提供的任何数组,而是返回一个 浅拷贝,其中包含与原始数组中的元素相同的元素。

¥The concat() method is a copying method. It does not alter this or any of the arrays provided as arguments but instead returns a shallow copy that contains the same elements as the ones from the original arrays.

如果任何源数组是 sparse,则 concat() 方法会保留空槽。

¥The concat() method preserves empty slots if any of the source arrays is sparse.

concat() 方法是 genericthis 值的处理方式与其他参数相同(除了它会首先转换为对象),这意味着普通对象将直接添加到结果数组中,而具有 true @@isConcatSpreadable 的类数组对象将被传播到 结果数组。

¥The concat() method is generic. The this value is treated in the same way as the other arguments (except it will be converted to an object first), which means plain objects will be directly prepended to the resulting array, while array-like objects with truthy @@isConcatSpreadable will be spread into the resulting array.

示例

¥Examples

连接两个数组

¥Concatenating two arrays

以下代码连接两个数组:

¥The following code concatenates two arrays:

js
const letters = ["a", "b", "c"];
const numbers = [1, 2, 3];

const alphaNumeric = letters.concat(numbers);
console.log(alphaNumeric);
// results in ['a', 'b', 'c', 1, 2, 3]

连接三个数组

¥Concatenating three arrays

以下代码连接三个数组:

¥The following code concatenates three arrays:

js
const num1 = [1, 2, 3];
const num2 = [4, 5, 6];
const num3 = [7, 8, 9];

const numbers = num1.concat(num2, num3);

console.log(numbers);
// results in [1, 2, 3, 4, 5, 6, 7, 8, 9]

将值连接到数组

¥Concatenating values to an array

以下代码将三个值连接到一个数组:

¥The following code concatenates three values to an array:

js
const letters = ["a", "b", "c"];

const alphaNumeric = letters.concat(1, [2, 3]);

console.log(alphaNumeric);
// results in ['a', 'b', 'c', 1, 2, 3]

连接嵌套数组

¥Concatenating nested arrays

以下代码连接嵌套数组并演示引用的保留:

¥The following code concatenates nested arrays and demonstrates retention of references:

js
const num1 = [[1]];
const num2 = [2, [3]];

const numbers = num1.concat(num2);

console.log(numbers);
// results in [[1], 2, [3]]

// modify the first element of num1
num1[0].push(4);

console.log(numbers);
// results in [[1, 4], 2, [3]]

使用 Symbol.isConcatSpread 连接类似数组的对象

¥Concatenating array-like objects with Symbol.isConcatSpreadable

默认情况下,concat 不会将所有类似数组的对象视为数组 - 仅当 Symbol.isConcatSpreadable 设置为真值时(例如 true)。

¥concat does not treat all array-like objects as arrays by default — only if Symbol.isConcatSpreadable is set to a truthy value (e.g. true).

js
const obj1 = { 0: 1, 1: 2, 2: 3, length: 3 };
const obj2 = { 0: 1, 1: 2, 2: 3, length: 3, [Symbol.isConcatSpreadable]: true };
console.log([0].concat(obj1, obj2));
// [ 0, { '0': 1, '1': 2, '2': 3, length: 3 }, 1, 2, 3 ]

在稀疏数组上使用 concat()

¥Using concat() on sparse arrays

如果任何源数组是稀疏的,则结果数组也将是稀疏的:

¥If any of the source arrays is sparse, the resulting array will also be sparse:

js
console.log([1, , 3].concat([4, 5])); // [1, empty, 3, 4, 5]
console.log([1, 2].concat([3, , 5])); // [1, 2, 3, empty, 5]

对非数组对象调用 concat()

¥Calling concat() on non-array objects

如果 this 值不是数组,则会将其转换为对象,然后按照与 concat() 的参数相同的方式进行处理。在这种情况下,返回值始终是一个普通的新数组。

¥If the this value is not an array, it is converted to an object and then treated in the same way as the arguments for concat(). In this case the return value is always a plain new array.

js
console.log(Array.prototype.concat.call({}, 1, 2, 3)); // [{}, 1, 2, 3]
console.log(Array.prototype.concat.call(1, 2, 3)); // [ [Number: 1], 2, 3 ]
const arrayLike = {
  [Symbol.isConcatSpreadable]: true,
  length: 2,
  0: 1,
  1: 2,
  2: 99, // ignored by concat() since length is 2
};
console.log(Array.prototype.concat.call(arrayLike, 3, 4)); // [1, 2, 3, 4]

规范

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

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看