Merge multiple arrays to one array in jquery

梦想与她 提交于 2019-12-06 08:44:35

问题


I am trying to merge my multiple arrays to one array using jquery. I know that we can merge two arrays to one using jquery merge function. And we can loop through those arrays and join them to one too. But I just wanted to know whether there is any other way to achieve this without using any loop. I am handling large number of datas. So I can expect any number of arrays too. I am worrying if we use a loop, it may affect the performance. Please give me some suggestions. Thanks in advance.

Kindest Regards Sibeesh Venu


回答1:


use .concat

and to apply it for multiple array like below

var multipleArrays = [[1,2], [3,4], [5,6]];

var flatArray = [].concat.apply([], multipleArrays); 
// [1, 2, 3, 4, 5, 6]

// Using the Spread syntax
var flatArray2 = Array.prototype.concat(...multipleArrays);
// [1, 2, 3, 4, 5, 6]



回答2:


array1.concat(array2) in plain javascript




回答3:


You can just push all values from one to another

var a = [1,2,3]
var b = [2,3,4,5]
a.push.apply(a, b)
console.log(a);// [1, 2, 3, 2, 3, 4, 5]


来源:https://stackoverflow.com/questions/32066001/merge-multiple-arrays-to-one-array-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!