JavaScript的推数组值到另一个数组 [英] Javascript push array values into another array

查看:188
本文介绍了JavaScript的推数组值到另一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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 values 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中,通过加1的值1?

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 values 1 by 1?

推荐答案

使用 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).

这篇关于JavaScript的推数组值到另一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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