将数组项目复制到另一个数组中 [英] Copy array items into another array
问题描述
我有一个JavaScript数组dataArray
,我想将其推送到新的数组newArray
中.除了我不希望newArray[0]
是dataArray
.我想将所有项目推入新数组:
I have a JavaScript array dataArray
which I want to push into a new array newArray
. Except I don't want newArray[0]
to be dataArray
. I want to push in all the items into the new array:
var newArray = [];
newArray.pushValues(dataArray1);
newArray.pushValues(dataArray2);
// ...
甚至更好:
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
);
因此,新数组现在包含各个数据数组的所有值.是否有类似pushValues
的速记可用,所以我不必遍历每个单独的dataArray
,逐项添加项目?
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?
推荐答案
使用 concat 函数,如下所示:
var arrayA = [1, 2];
var arrayB = [3, 4];
var newArray = arrayA.concat(arrayB);
newArray
的值将为[1, 2, 3, 4]
(arrayA
和arrayB
保持不变; concat
创建并为结果返回一个新数组).
The value of newArray
will be [1, 2, 3, 4]
(arrayA
and arrayB
remain unchanged; concat
creates and returns a new array for the result).
这篇关于将数组项目复制到另一个数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!