Home > Web Front-end > JS Tutorial > body text

How to merge two arrays in javascript

王林
Release: 2020-05-29 16:47:11
forward
3647 people have browsed it

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];
Copy after login
var c = a.concat(b);

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

Method 2: Use loop

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

Method 3: Use apply

a.push.apply(a,b);
Copy after login

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template