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

[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]

其他回答

没有为数组定义+运算符。

Javascript将数组转换为字符串并将它们连接起来。

 

更新

由于这个问题和我的回答得到了很多关注,我觉得对+操作符的一般行为有一个概述是有用的和相关的。

所以,开始吧。

除了E4X和特定于实现的东西,Javascript(从ES5开始)有6种内置数据类型:

未定义的 零 布尔 数量 字符串 对象

注意,虽然typeof返回Null对象和可调用对象的函数有点令人困惑,但Null实际上不是一个对象,严格来说,在符合规范的Javascript实现中,所有函数都被认为是对象。

这是对的——Javascript没有原始数组;只有一个名为Array的对象的实例,并使用一些语法糖来减轻痛苦。

更令人困惑的是,诸如new Number(5)、new Boolean(true)和new String("abc")等包装器实体都是对象类型,而不是人们可能期望的数字、布尔值或字符串。然而,对于算术运算符,数字和布尔值表现为数字。

容易吧,对不对?了解了所有这些之后,我们可以进入概述本身。

根据操作数类型,不同的+结果类型

            || undefined | null   | boolean | number | string | object |
=========================================================================
 undefined  || number    | number | number  | number | string | string | 
 null       || number    | number | number  | number | string | string | 
 boolean    || number    | number | number  | number | string | string | 
 number     || number    | number | number  | number | string | string | 
 string     || string    | string | string  | string | string | string | 
 object     || string    | string | string  | string | string | string | 

*适用于Chrome13, FF6, Opera11和IE9。检查其他浏览器和版本留给读者练习。

注意:正如CMS所指出的,对于某些情况下的对象,如数字,布尔和自定义对象,+运算符不一定会产生字符串结果。它可以根据对象到原语转换的实现而变化。例如var o = {valueOf:function(){返回4;}};求o + 2;得到6,一个数字,求o + '2'得到'42',一个字符串。

要查看总览表是如何生成的,请访问http://jsfiddle.net/1obxuc7m/

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总是将数组转换为字符串,从而产生这样的结果。

这完全是你让它做的。

你加在一起的是数组引用(JS将其转换为字符串),而不是看起来的数字。这有点像字符串相加:"hello " + "world" = "hello world"

看起来JavaScript正在将数组转换为字符串并将它们连接在一起。如果要将元组添加到一起,则必须使用循环或映射函数。

它将单个数组转换为字符串,然后组合字符串。