Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么';数组连接在Javascript中是否有效?_Javascript_Arrays - Fatal编程技术网

为什么';数组连接在Javascript中是否有效?

为什么';数组连接在Javascript中是否有效?,javascript,arrays,Javascript,Arrays,当我在javascript中创建两个数组并尝试使用“concat”关键字连接它们时,生成的数组总是空的(好吧,应该插入的数组中的内容不会插入)。我无法想象js是如何工作的,因为。。。如果concat关键字什么都不做,它的意义何在。哈哈 那么我一定是做错了什么,但我完全按照文件办事 下面是一把小提琴,它展示了我的问题: 我的意思是,控制台输出如下: 另外,在我的实际项目中,我使用的是angular 1.4,因此,如果有一种方法可以使用它来压缩数组,我愿意使用它。.concat()创建一个新数组并

当我在javascript中创建两个数组并尝试使用“concat”关键字连接它们时,生成的数组总是空的(好吧,应该插入的数组中的内容不会插入)。我无法想象js是如何工作的,因为。。。如果concat关键字什么都不做,它的意义何在。哈哈

那么我一定是做错了什么,但我完全按照文件办事

下面是一把小提琴,它展示了我的问题:

我的意思是,控制台输出如下:

另外,在我的实际项目中,我使用的是angular 1.4,因此,如果有一种方法可以使用它来压缩数组,我愿意使用它。

.concat()
创建一个新数组并返回它。它不会将元素添加到现有数组中

发件人:

concat将创建一个新数组,该数组由上的对象中的元素组成 它被调用,对于每个参数,后面依次是 该参数的元素(如果该参数是数组)或 参数本身(如果参数不是数组)

concat不会更改此数组或作为参数提供的任何数组 而是返回一个浅层副本,其中包含相同的副本 从原始数组组合的元素。原作的要素 将数组复制到新数组中,如下所示:

可以使用
.splice()
.push()
将元素添加到现有数组中

var-greetings2=[“肯定”、“是”];
var obArray2=[“嗯?”];
obArray2.push.apply(obArray2,greetings2);
//在代码段中显示结果
document.write(JSON.stringify(obArray2))
.concat()
创建一个新数组并返回它。它不会将元素添加到现有数组中

发件人:

concat将创建一个新数组,该数组由上的对象中的元素组成 它被调用,对于每个参数,后面依次是 该参数的元素(如果该参数是数组)或 参数本身(如果参数不是数组)

concat不会更改此数组或作为参数提供的任何数组 而是返回一个浅层副本,其中包含相同的副本 从原始数组组合的元素。原作的要素 将数组复制到新数组中,如下所示:

可以使用
.splice()
.push()
将元素添加到现有数组中

var-greetings2=[“肯定”、“是”];
var obArray2=[“嗯?”];
obArray2.push.apply(obArray2,greetings2);
//在代码段中显示结果
document.write(JSON.stringify(obArray2))请尝试以下操作:

var greetings = [{"affirmative":"yeah"}];
var exclamations = [{"omg":"Oh my golly-gee-wilickers!"}, {"wowz":"wowzers!"}];

var obArray = Array.prototype.concat.apply([], greetings, exclamations);
console.log("The jumble array is: " + obArray);
//Console Output: The jumble array is: [object Object]

var greetings2 = ["affirmative","yeah"];
var exclamations2 = ["omg"];

var obArray2 = [].concat(greetings2, exclamations2);
console.log("The jumble array is: " + obArray2);
//Console Output: The jumble array is: affirmative,yeah,omg
请尝试以下操作:

var greetings = [{"affirmative":"yeah"}];
var exclamations = [{"omg":"Oh my golly-gee-wilickers!"}, {"wowz":"wowzers!"}];

var obArray = Array.prototype.concat.apply([], greetings, exclamations);
console.log("The jumble array is: " + obArray);
//Console Output: The jumble array is: [object Object]

var greetings2 = ["affirmative","yeah"];
var exclamations2 = ["omg"];

var obArray2 = [].concat(greetings2, exclamations2);
console.log("The jumble array is: " + obArray2);
//Console Output: The jumble array is: affirmative,yeah,omg

正如其他人所说,
.concat
返回一个新数组,并且不会改变正在使用的数组的原始状态。如果希望通过
.concat
将两个数组的值连接起来,则必须将其存储在变量中,或者只需在需要的地方进行连接即可

例如:

var greetings = [{"affirmative":"yeah"}];
var exclamations = [{"omg":"Oh my golly-gee-wilickers!"},{"wowz":"wowzers!"}];

var obArray = greetings.concat(exclamations);

console.log(obArray); // returns [obj, obj, obj]
这将给您相同的结果:

console.log(greetings.concat(exclamations));

最后一件事。像
.concat
这样的方法是可链接的。

像其他人所说的那样,
.concat
返回一个新数组,并且不会改变正在使用的数组的原始状态。如果希望通过
.concat
将两个数组的值连接起来,则必须将其存储在变量中,或者只需在需要的地方进行连接即可

例如:

var greetings = [{"affirmative":"yeah"}];
var exclamations = [{"omg":"Oh my golly-gee-wilickers!"},{"wowz":"wowzers!"}];

var obArray = greetings.concat(exclamations);

console.log(obArray); // returns [obj, obj, obj]
这将给您相同的结果:

console.log(greetings.concat(exclamations));

最后一件事。像
.concat
这样的方法是链式的。

concat
返回一个新数组,它不会变异。^
obArray2=obArray2.concat(greetings2)
另外,
Function.prototype.call的第一个参数定义调用的上下文(this
的值),而不是要调用的第一个数组concatenate@Sebas是的,但事实证明,在这种情况下是正确的;第一个例子只是多余的。
concat
返回一个新数组,它不会变异。^
obArray2=obArray2.concat(greetings2)
另外,
Function.prototype.call
的第一个参数定义调用的上下文(this
的值),而不是要调用的第一个数组concatenate@Sebas是的,但事实证明,在这种情况下是正确的;第一个例子只是多余的。