Wednesday, July 3, 2024
HomeLanguagesJavascriptWhat is the most efficient way to concatenate N arrays in JavaScript...

What is the most efficient way to concatenate N arrays in JavaScript ?

In this article, we will see how to concatenate N arrays in JavaScript. The efficient way to concatenate N arrays can depend on the number of arrays and the size of arrays.

To concatenate N arrays, we use the following methods:

Method 1: Using push() Method

We create the N arrays and then concatenate the array by using the push() method. In this, we also use the spread operator(…) which iterates over each of the values.

Javascript




const arr1 = [11, 12, 13];
const arr2 = [14, 15, 16];
arr1.push(...arr2);
console.log(arr1);


Output

[ 11, 12, 13, 14, 15, 16 ]

Method 2: Using concat() Method

First, we will create N arrays and then apply the concat() method to concatenate N arrays. The concat() method is used to merge N arrays together. This method does not alter the original arrays and returns the new Array.

Example:

Javascript




const arr1 = [11, 12, 13],
      arr2 = [14, 15, 16],
      arr3 = [17, 18, 19],
      arr4 = [1, 3, 7];
 
const arr = arr1.concat(arr2, arr3, arr4);
 
console.log(arr);


Output

[
  11, 12, 13, 14, 15,
  16, 17, 18, 19,  1,
   3,  7
]


Method 3: Using Spread Operator

You can also use Spread Operator to concatenate N arrays. For this, first, we will create N arrays and then apply Spread Operator to concatenate N arrays. 

Note: This method is faster and less memory intensive than the concat() method.

Example:

Javascript




const arr1 = [11, 12, 13],
      arr2 = [14, 15, 16],
      arr3 = [17, 18, 19],
      arr4 = [1, 3, 7];
 
const arr = [...arr1, ...arr2, ...arr3, ...arr4];
 
console.log(arr);


Output

[
  11, 12, 13, 14, 15,
  16, 17, 18, 19,  1,
   3,  7
]


Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments