本文翻译自:Copy array items into another array
I have a JavaScript array dataArray
which I want to push into a new array newArray
. 我有一个JavaScript数组dataArray
,我想将其推入一个新的数组newArray
。 Except I don't want newArray[0]
to be dataArray
. 除了我不希望newArray[0]
成为dataArray
。 I want to push in all the items into the new array: 我想将所有项目推入新数组:
var newArray = [];
newArray.pushValues(dataArray1);
newArray.pushValues(dataArray2);
// ...
or even better: 甚至更好:
var newArray = new Array (
dataArray1.values(),
dataArray2.values(),
// ... where values() (or something equivalent) would push the individual values into the array, rather than the array itself
);
So now the new array contains all the values of the individual data arrays. 所以现在新数组包含各个数据数组的所有值。 Is there some shorthand like pushValues
available so I don't have to iterate over each individual dataArray
, adding the items one by one? 有没有类似pushValues
简写,所以我不必迭代每个dataArray
,逐个添加项目?
#1楼
参考:https://stackoom.com/question/HRBt/将数组项复制到另一个数组中
#2楼
instead of push() function use concat function for IE. 而不是push()函数使用IE的concat函数。 example, 例,
var a=a.concat(a,new Array('amin'));
#3楼
Тhis is a working code and it works fine: 这是一个工作代码,它工作正常:
var els = document.getElementsByTagName('input'), i;
var invnum = new Array();
var k = els.length;
for(i = 0; i < k; i++){invnum.push(new Array(els[i].id,els[i].value))}
#4楼
I will add one more "future-proof" reply 我将再添加一个“面向未来”的回复
In ECMAScript 6, you can use the Spread syntax : 在ECMAScript 6中,您可以使用Spread语法 :
let arr1 = [0, 1, 2]; let arr2 = [3, 4, 5]; arr1.push(...arr2); console.log(arr1)
Spread syntax is not yet included in all major browsers. 扩展语法尚未包含在所有主流浏览器中。 For the current compatibility, see this (continuously updated) compatibility table . 有关当前兼容性,请参阅此(不断更新的) 兼容性表 。
You can, however, use spread syntax with Babel.js . 但是,您可以使用Babel.js的扩展语法。
edit: 编辑:
See Jack Giffin's reply below for more comments on performance. 有关性能的更多评论,请参阅下面的Jack Giffin的回复。 It seems concat is still better and faster than spread operator. 似乎concat仍然比spread运算符更好更快。
#5楼
Found an elegant way from MDN 从MDN找到一种优雅的方式
var vegetables = ['parsnip', 'potato'];
var moreVegs = ['celery', 'beetroot'];
// Merge the second array into the first one
// Equivalent to vegetables.push('celery', 'beetroot');
Array.prototype.push.apply(vegetables, moreVegs);
console.log(vegetables); // ['parsnip', 'potato', 'celery', 'beetroot']
Or you can use the spread operator
feature of ES6: 或者您可以使用ES6的spread operator
功能:
let fruits = [ 'apple', 'banana'];
const moreFruits = [ 'orange', 'plum' ];
fruits.push(...moreFruits); // ["apple", "banana", "orange", "plum"]
#6楼
The following seems simplest to me: 以下对我来说似乎最简单:
var newArray = dataArray1.slice();
newArray.push.apply(newArray, dataArray2);
As "push" takes a variable number of arguments, you can use the apply
method of the push
function to push all of the elements of another array. 由于“push”采用可变数量的参数,因此可以使用push
函数的apply
方法来推送另一个数组的所有元素。 It constructs a call to push using its first argument ("newArray" here) as "this" and the elements of the array as the remaining arguments. 它使用其第一个参数(此处为“newArray”)构造一个调用push作为“this”,并将数组元素作为其余参数。
The slice
in the first statement gets a copy of the first array, so you don't modify it. 第一个语句中的slice
获取第一个数组的副本,因此不要修改它。
Update If you are using a version of javascript with slice available, you can simplify the push
expression to: 更新如果您使用的是带有切片的javascript版本,则可以push
表达式简化为:
newArray.push(...dataArray2)