DEV Community

Ashutosh Kumar Singh
Ashutosh Kumar Singh

Posted on • Originally published at Medium on

2 1

3 Different ways to cOmbine Arrays in Javascript

3 Different ways to combine Arrays in Javascript

Hi everyone this is my second post, and I am very excited, in this post I will talk about different methods of combining two or more arrays. Before We get started, check out my first post on how to create a twitter bot in javascript in a few steps.

Create a simple yet effective Twitter BOt in nOdejs (javascript)

So let’s get started.

I am going to use Visual Studio code editor,

1. Concat()

The most basic way is using the concat method and concating two different arrays

Its very simple, you just have to define two arrays and combine them as shown

Remember the sequence matters

let firstArray = [1,2,3,'Shinchan']
let secondArray = ['Nohara',4,5,6]
let combinedArray1 = firstArray.concat(secondArray)
let combinedArray2 = secondArray.concat(firstArray)
console.log(`Combined Array1 = `+combinedArray1)
// Combined Array1 = 1,2,3,Shinchan,Nohara,4,5,6
console.log(`Combined Array2 = `+combinedArray2)
//Combined Array2= Nohara,4,5,6,1,2,3,Shinchan
view raw concat.js hosted with ❤ by GitHub

OR

Another way to use concat is by concating two or more arrays in an empty array

let firstArray = [1,2,3,'Shinchan']
let secondArray = ['Nohara',4,5,6]
let thirdArray = [7,8,9,'Himawari']
let combinedArray = [].concat(firstArray,secondArray,thirdArray)
console.log(`Combined Array = `+combinedArray)
//Combined Array=1,2,3,Shinchan,Nohara,4,5,6,7,8,9,Himawari
view raw concat2.js hosted with ❤ by GitHub

2. using … (Spread Operator)(ES6)(shortcut)

Now the second method is like a shortcut, you just have to store the values of two or more arrays in a different array using …(three dots) or spread operator, it is only available in ES6 version only and its very efficient way to combine arrays and is my personal favorite

let firstArray = [1, 2, 3, 'Shinchan']
let secondArray = ['Nohara', 4, 5, 6]
let thirdArray = [7, 8, 9, 'Himawari']
let combinedArray1 = [...firstArray, ...secondArray, ...thirdArray]
let combinedArray2 = [...secondArray, ...firstArray, ...thirdArray]
console.log('CombinedArray1 = ' + combinedArray1)
//Combined Array1 = 1,2,3,Shinchan,Nohara,4,5,6,7,8,9,Himawari
console.log('CombinedArray2 = ' + combinedArray2)
//Combined Array2 = 4,5,6,1,2,3,Shinchan,7,8,9
view raw spread.js hosted with ❤ by GitHub

As you can see, sequence matters here to

3. Merge Arrays with push

You can also use the push method to combine different arrays like as shown

let firstArray = [1, 2, 3, 'Shinchan']
let secondArray = ['Nohara', 4, 5, 6]
let thirdArray = [7, 8, 9, 'Himawari']
let combinedArray = []
combinedArray.push(...firstArray, ...secondArray, ...thirdArray)
console.log(`CombinedArray = `+combinedArray)
// CombinedArray = [1,2,3,'Shinchan','Nohara',4,5,6,7,8,9,'Himawari']
view raw push1.js hosted with ❤ by GitHub

As you can see first declare an empty array and then use push with … and the contents of the arrays will be copied in the desired array.

Remember if,

don't use … (spread operator), then the whole array will be pushed and you will get a nested array called Two-Dimensional Arrays (that is for another day’s discussion, so let’snot get into that)

let firstArray = [1, 2, 3, 'Shinchan']
let secondArray = ['Nohara', 4, 5, 6]
let thirdArray = [7, 8, 9, 'Himawari']
let combinedArray = []
combinedArray.push( firstArray, secondArray, thirdArray)
console.log(`CombinedArray = `+combinedArray)
/* CombinedArray = [ [1,2,3,'Shinchan'],
['Nohara',4,5,6],
[7,8,9,'Himawari'] ]
view raw push2.js hosted with ❤ by GitHub

That’s all thanks for being with me if you think there are any errors or you want to insight some information feel free to comment below


Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay