Home  >  Article  >  Web Front-end  >  How to merge two arrays in javascript

How to merge two arrays in javascript

王林
王林forward
2020-05-29 16:47:113646browse

How to merge two arrays in javascript

Methods to merge two arrays are:

Method 1: Use concat

var a = [1,2,3];
var b = [4,5,6];
var c = a.concat(b);

//c=[1,2,3,4,5,6];

Method 2: Use loop

for(let i in b){
  a.push(b[i])
}

Method 3: Use apply

a.push.apply(a,b);

When merging arrays, you can first determine the size of the arrays. It will obviously be faster to merge the larger ones with the smaller ones.

If you don’t want to change the array, It is recommended to use the concat method.

Recommended tutorial: js introductory tutorial

The above is the detailed content of How to merge two arrays in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete