Arrays are everywhere. One cannot code without learning arrays. There comes a point where we have to merge several arrays and the question lies how ?
Merging has to be efficient, scalable and easy.
Well, javascript has got you covered.
Lets say you have two arrays to merge,
let a = [1,2,3]
let b = [4,5,6]We can use the concat() function and merge them, a = a.concat(b);
console.log(a);a = [1,2,3,4,5,6]
Now, lets say we do not know how many arrays we have to merge, it can be 3 or 4, can have many elements.
In such a scenario using the spread operator (…) makes a huge difference.
let a = [1,2,3]
let b = [4,5,6]
let c = [7,8,9]using spread operatorlet result = [..a,..b,...c]
console.log(result);result = [1,2,3,4,5,6,7,8,9]
So easy and simple.
Make sure to use to use it on your code when you you need to merge arrays.
Thank you for reading !