AshKeys

Confessions and Confusions of Ashok M A. Personal and Professional Blog.

Ashok Mannolu Arunachalam, How toArraysJSES6
Back

How to merge arrays in JavaScript

The concat method

With concat, we do not have to add even a single for...in loop. Instead, we can simply chain the concat method in the order we want!

js
const array1 = [1, 2, 3]
const array2 = [4, 5, 6]
const array3 = [7, 8, 9]
const mergedArray = [].concat(array1)
.concat(array2).concat(array3)
console.log(mergedArray) // [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

Prefer starting with empty array when chaining concat. In this way, it is clear that we make a new array and do not manipulate the existing ones! ^_^

Spread Operator (Since ES6)

With ES6 it is simple now! We can merge two or more arrays in one line:

js
const array1 = [1, 2, 3]
const array2 = [4, 5, 6]
const array3 = [7, 8, 9]
const mergedArray = [...array1, ...array2, ...array3]
console.log(mergedArray) // [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]