我想将一个数组的元素添加到另一个数组中,所以我尝试了这样做:

[1,2] + [3,4]

它的答复是:

"1,23,4"

这是怎么回事?


当前回答

Some answers here have explained how the unexpected undesired output ('1,23,4') happens and some have explained how to obtain what they assume to be the expected desired output ([1,2,3,4]), i.e. array concatenation. However, the nature of the expected desired output is actually somewhat ambiguous because the original question simply states "I wanted to add the elements of an array into another...". That could mean array concatenation but it could also mean tuple addition (e.g. here and here), i.e. adding the scalar values of elements in one array to the scalar values of the corresponding elements in the second, e.g. combining [1,2] and [3,4] to obtain [4,6].

假设两个数组都有相同的arity/length,这里有一个简单的解决方案:

Const arr1 = [1,2]; Const arr2 = [3,4]; Const add = (a1, a2) => a1。Map ((e, i) => e + a2[i]); console.log(添加(arr1 arr2));// ==> [4,6]

其他回答

另一个使用简单的“+”符号的结果是:

[1,2]+','+[3,4] === [1,2,3,4]

所以像这样的东西应该工作(但是!):

var a=[1,2];
var b=[3,4];
a=a+','+b; // [1,2,3,4]

... 但它会将变量a从数组转换为字符串!记住这一点。

Some answers here have explained how the unexpected undesired output ('1,23,4') happens and some have explained how to obtain what they assume to be the expected desired output ([1,2,3,4]), i.e. array concatenation. However, the nature of the expected desired output is actually somewhat ambiguous because the original question simply states "I wanted to add the elements of an array into another...". That could mean array concatenation but it could also mean tuple addition (e.g. here and here), i.e. adding the scalar values of elements in one array to the scalar values of the corresponding elements in the second, e.g. combining [1,2] and [3,4] to obtain [4,6].

假设两个数组都有相同的arity/length,这里有一个简单的解决方案:

Const arr1 = [1,2]; Const arr2 = [3,4]; Const add = (a1, a2) => a1。Map ((e, i) => e + a2[i]); console.log(添加(arr1 arr2));// ==> [4,6]

JavaScript的+运算符有两个目的:将两个数字相加,或者连接两个字符串。它对数组没有特定的行为,所以它将它们转换为字符串,然后连接它们。

如果你想连接两个数组来生成一个新的数组,请使用.concat方法:

[1, 2].concat([3, 4]) // [1, 2, 3, 4]

如果你想有效地将所有元素从一个数组添加到另一个数组,你需要使用.push方法:

var data = [1, 2];

// ES6+:
data.push(...[3, 4]);
// or legacy:
Array.prototype.push.apply(data, [3, 4]);

// data is now [1, 2, 3, 4]

+运算符的行为在ECMA-262 5e章节11.6.1中定义:

11.6.1 The Addition operator ( + ) The addition operator either performs string concatenation or numeric addition. The production AdditiveExpression : AdditiveExpression + MultiplicativeExpression is evaluated as follows: Let lref be the result of evaluating AdditiveExpression. Let lval be GetValue(lref). Let rref be the result of evaluating MultiplicativeExpression. Let rval be GetValue(rref). Let lprim be ToPrimitive(lval). Let rprim be ToPrimitive(rval). If Type(lprim) is String or Type(rprim) is String, then Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim) Return the result of applying the addition operation to ToNumber(lprim) and ToNumber(rprim). See the Note below 11.6.3.

可以看到每个操作数都被转换为原始数。通过进一步阅读,我们可以发现ToPrimitive总是将数组转换为字符串,从而产生这样的结果。

+连接字符串,因此它将数组转换为字符串。

[1,2] + [3,4]
'1,2' + '3,4'
1,23,4

要组合数组,使用concat。

[1,2].concat([3,4])
[1,2,3,4]

[1,2]+[3,4]在JavaScript中等同于求值:

new Array( [1,2] ).toString() + new Array( [3,4] ).toString();

所以为了解决你的问题,最好的方法是在原地添加两个数组,或者不创建一个新数组:

var a=[1,2];
var b=[3,4];
a.push.apply(a, b);