为什么数组串联不能在Javascript中使用? [英] Why Doesn't Array Concatenation Work in Javascript?

查看:71
本文介绍了为什么数组串联不能在Javascript中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在javascript中创建两个数组并尝试使用'concat'关键字将它们连接时,结果数组始终为空(嗯,应该插入的数组中的内容未插入).我无法想象这实际上应该是js的工作方式,因为那样吧……如果concat关键字不执行任何操作,那将是什么呢?哈哈.

因此,我必须做错了事,但我完全在关注文档.

这是一个小提琴,演示了我的问题:

PS.在我的真实项目中,我使用的是angular 1.4,因此,如果有一种用它来连接数组的方法,我就可以接受.

解决方案

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

来自 MDN :

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

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

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

  var greetings2 = [肯定",是"];var obArray2 = [嗯?"];obArray2.push.apply(obArray2,greetings2);//在片段中显示结果document.write(JSON.stringify(obArray2));  


或者,只需使用从 .concat()返回的新数组:

  var greetings2 = [肯定",是"];var obArray2 = [嗯?"];var newArray = obArray2.concat(greetings2);//在片段中显示结果document.write(JSON.stringify(newArray));  

When I create two arrays in javascript and try to concatenate them using the 'concat' keyword, the resulting array is always empty (well, the things from the array that should be inserted are not inserted). I couldn't imagine this is actually how js is supposed to work because then... what would be the point of the concat keyword if it does nothing. haha.

So then I must be doing something wrong, but I'm following the docs exactly.

Here's a fiddle that demonstrates my issue: https://jsfiddle.net/webwhizjim/7z4v33of/

// Example with objects- doesn't work...
var greetings = [{"affirmative":"yeah"}];
var exclamations = [{"omg":"Oh my golly-gee-wilickers!"},{"wowz":"wowzers!"}];

var obArray = [];

obArray.concat(greetings);
console.log("The jumble array is: " + obArray);

// Example with strings- still doesn't work...

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

var obArray2 = ["huh?"];

[].concat.call(obArray2, greetings2);
console.log("The jumble array is: " + obArray2);

Just to be clear by "it doesn't work" I mean the console output is this:

PS. In my real project I'm using angular 1.4, so if there's a way to concat arrays with that I'm open to it.

解决方案

.concat() creates a new array and returns it. It does not add elements onto an existing array.

From MDN:

concat creates a new array consisting of the elements in the object on which it is called, followed in order by, for each argument, the elements of that argument (if the argument is an array) or the argument itself (if the argument is not an array).

concat does not alter this or any of the arrays provided as arguments but instead returns a shallow copy that contains copies of the same elements combined from the original arrays. Elements of the original arrays are copied into the new array as follows:

You can add elements onto an existing array with .splice() or with .push().

var greetings2 = ["affirmative","yeah"];
var obArray2 = ["huh?"];
obArray2.push.apply(obArray2, greetings2);

// display result in snippet
document.write(JSON.stringify(obArray2));


Or, just use the newly returned array from .concat():

    var greetings2 = ["affirmative","yeah"];
    var obArray2 = ["huh?"];
    var newArray = obArray2.concat(greetings2);

    // display result in snippet
    document.write(JSON.stringify(newArray));

这篇关于为什么数组串联不能在Javascript中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆