简体   繁体   English

如何在对象中连接Javascript数组

[英]How to concat javascript array in an object

could someone help me concat an array to another array that is part of a javascript object? 有人可以帮助我将一个数组连接到javascript对象中另一个数组吗? So far I have tried: 到目前为止,我已经尝试过:

var someObj = {};
someObj.someArray = ['1', '2', '3', '4'];

var randomArray = ['5', '6', '7']


someObj.someArray.concat(randomArray);

console.log(someObj);

However, the final object contains the original array, not the original array concated with the randomArray . 但是,最终对象包含原始数组,而不是与randomArray的原始数组。

Here's my fiddle: 这是我的小提琴:

http://jsfiddle.net/d8md3r7a/ http://jsfiddle.net/d8md3r7a/

Take a look at the MDN documentation for Array.prototype.concat : 查看Array.prototype.concatMDN文档

The concat() method is used to merge two or more arrays. concat()方法用于合并两个或多个数组。 This method does not change the existing arrays, but instead returns a new array . 此方法不更改现有数组,而是返回一个新数组 (Emphasis mine) (强调我的)

As you can see, the method does not actually change the array in place. 如您所见,该方法实际上并未更改数组。 The new array is returned from the method, so you'll need to assign the to the returned array like so: 新数组是从方法返回的,因此您需要将分配给返回的数组,如下所示:

someObj.someArray = someObj.someArray.concat(randomArray);

Array.concat() doesn't modify the original array but returns the concatenated results. Array.concat()不会修改原始数组,但会返回连接的结果。 You need to use the return value of Array.concat() to explicitly set the original object property. 您需要使用Array.concat()返回值来显式设置原始对象属性。 Updated your fiddle here . 在这里更新了小提琴。

You can do something like this instead of .concat() . 您可以这样做,而不是.concat()

 var someObj = {}; someObj.someArray = ['1', '2', '3', '4']; var randomArray = ['5', '6', '7'] randomArray.forEach(v => someObj.someArray.push(v)); console.log(someObj); 

concat makes a new array and is used like this concat制作一个新数组,并像这样使用

someObj.someArray = someObj.someArray.concat(randomArray);  

Another way is to modify the array in place by inserting the additional elements. 另一种方法是通过插入其他元素来修改数组。 To do that, use push together with Function.apply like this: 为此,将pushFunction.apply一起使用,如下所示:

[].push.apply(someObj.someArray, randomArray);  

Function.apply takes two parameters, the first is a this value and the second is an array that is used to fill in the function parameters. Function.apply具有两个参数,第一个是this值,第二个是用于填充函数参数的数组。 Here, Function.apply causes the elements of randomArray to be unwrapped from the array as (multiple) parameters to the push function, instead of pushing the entire array as a single element. 在这里, Function.apply导致将randomArray的元素作为(多个)参数从数组中解包到push函数,而不是将整个数组作为单个元素进行推送。 The [] is a trick to create a blank array to grab the push function, instead of the more verbose someObj.someArray.push or Array.prototype.push . []是创建空白数组以获取push函数的技巧,而不是更冗长的someObj.someArray.pushArray.prototype.push

To echo the answer above. 回应上面的答案。 The use of the .concat() method is definitely the way to go. .concat()方法的使用绝对是正确的方法。 You could also use the ES6 spread operator which can be used like push and concat. 您还可以使用ES6传播运算符,该运算符可以像push和concat一样使用。 This will give you the results you are looking for. 这将为您提供所需的结果。

var someObj = {};
someObj.someArray = ['1', '2', '3', '4'];

var randomArray = ['5', '6', '7']


someObj.someArray.push(...randomArray);

console.log(someObj);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM