本文翻译自: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)

将数组项复制到另一个数组中相关推荐

  1. java复制数组到数组,Java如何将一个数组元素复制到另一个数组

    Java如何将一个数组元素复制到另一个数组 1 说明 在此程序中,我们需要将一个数组的所有元素复制到另一个数组中.这可以通过循环遍历第一数组并将第一数组的元素存储到第二数组的相应位置来完成. 2 算法 ...

  2. c语言把一个数组赋值给另一个数组_如何把一个固定数组的值传递给另外一个数组...

    大家好,今日我们继续讲解VBA数组与字典解决方案,今日讲解的是第34讲:数组的传递.在应用数组的时候,我们往往需要要把数组的值由一个数组传递给另外一个数组,就如同变量的传递一样: A=B '把B值赋给 ...

  3. php数组的值传递给另一个数组,如何把一个固定数组的值传递给另外一个数组

    大家好,今日我们继续讲解VBA数组与字典解决方案,今日讲解的是第34讲:数组的传递.在应用数组的时候,我们往往需要要把数组的值由一个数组传递给另外一个数组,就如同变量的传递一样: A=B '把B值赋给 ...

  4. php判断一个数组是否存在在另一个数组中

    /*** ** 判断一个数组是否存在于另一个数组中** @param $arr* @param $allArr* @return boolean*/ function isAllExists($arr ...

  5. Python-将一个列表的数据复制到另一个列表中

    # 题目:将一个列表的数据复制到另一个列表中 list1 = [x for x in range(1,5)]list2 = []print(list1)print(list2) #初始化 print( ...

  6. C语言 | 将字符串中的元音字母复制到另一个字符串中

    例70:C语言写一个函数,将一个字符串中的元音字母复制到另一字符串,然后输出. 解析:if语句判断一下每一个字母是否符合元音字母,读者看着道题的时候,需要注意一点的是如果用scanf函数是否可以,思考 ...

  7. 将磁盘上的一个文本文件的内容复制到另一个文件中

    <程序设计基础实训指导教程-c语言> ISBN 978-7-03-032846-5 p198 8.1.2 上级实训内容 [实训内容2]将磁盘上的一个文本文件的内容复制到另一个文件中 #in ...

  8. 萌新的Python练习实例100例(七)将一个列表的数据复制到另一个列表中

    题目: 将一个列表的数据复制到另一个列表中 分析: · 这道题是联系list和切片的使用: 方法1: · 将b的值赋予a: · a[0:3]表示使用切片从0位置开始到第3位置结束: · a[:3]表示 ...

  9. sql如何把一个数据库的表复制到另一个数据库中【转载】

    sql如何把一个数据库的表复制到另一个数据库中[转载] 这篇经验帖是我转载的,是我搜到的最为直观具体的 一.需要把一张表的表结构从一个数据(A)库复制到另一个数据库(B).操作步奏如下: ①在数据库A ...

最新文章

  1. Java--日期的使用
  2. cocos2d-x 3.x 场景切换特效大集合
  3. time函数及其用法
  4. 我爱我家 CIO 刘东颖:如何靠六大维度提升“数字化”能力?|鲸犀峰会
  5. 【算法竞赛学习】二手车交易价格预测-Task5模型融合
  6. STL源码剖析 set
  7. 上传Text文档并转换为PDF(解决乱码)
  8. 解决问题的能力 > 10倍程序员
  9. java中super和this_Java中this和super的用法总结
  10. Tensorflow saved_model.pb 文件转成 saved_model.pbtxt文件
  11. java下载不了_教大家电脑java安装不了怎么办
  12. TB6612FNG 驱动学习笔记
  13. NDK 开发实战 - 微信公众号二维码检测
  14. 神仙项目,轻松上手了解前后端分离!
  15. 双显示器LOL加载游戏提示error无法初始化图形设备解决方法
  16. Linux——用户账号和管理
  17. win11使用移动硬盘(固态非固态)卡顿问题解决
  18. 计算机博士论文 评阅意见,博士论文评阅意见
  19. MathType 6.9中平行四边形符号如何输入
  20. 将idea初学者配置_初学者怪胎:如何将ISO映像刻录到光盘

热门文章

  1. 驱动,包括很多软件,并不是最新的就是最好的
  2. 【编译打包】nginx_1.6.2-1~precise.debian.tar.gz
  3. Oracle RAC Failover机制分析
  4. 百度缺的不是狼性,而是鲁滨逊
  5. 2012-09-16-html
  6. Charles 映射本地文件map local
  7. 换工位解决ssh 卡住的背后
  8. 工程师追查线上问题(或运维)常用的shell命令
  9. Reapter 中客户端控件和服务器端控件的选择
  10. Windows Media Services 9 系列常见问题解答