Merge N arrays containing arrays in javascript

I have an array which contains arrays too. this array is template

template has 2 arrays, but 2 is not fixed, it can go on N number of arrays which also contains arrays. this is what I tried:

const template =, i) => {
  return healthpackItem.health_pack_templates
console.log('template', template);
var c = [];
  for (var i = 0; i >= template.length; i++) {

c does only returns [] and not an array with 0,1,2,3,4,5,6,7,8,9 arrays inside. What am I doing wrong?

what I want to happen should be like this: [array 0,1,2,3,4,5,6,7,8,9] after it is merged.

Try using flat() method

The flat() method which creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

What is the most efficient way to concatenate N arrays?, What is the most efficient way to concatenate N arrays of objects in JavaScript? The arrays are mutable, and the result can be stored in one of the input arrays. Using push() Method: The push() method is used with spread operator to pushes the elements of arrays into the existing array. It is especially helpful when we need to append a value to an existing array and need not return a new array. Note: If spread operator is not used, then arrays as a whole are appended.

Maybe because you wrote i>=template.length. it should brake your for loop immediately.

2 Ways to Merge Arrays in JavaScript, Here are 2 ways to combine your arrays and return a NEW array. As you can see, this way of writing it doesn't manipulate or change the existing array. In JavaScript there are manyways to merge or combine arrays let’s learn the most commonly used methods to merging arrays. Array.Concat( ) method. JavaScript concat method creates the new array by merging two or more arrays instead of mutating the existing arrays.

Merge N Arrays

There are multiple ways in javascript today to merge variable number of arrays

Option 1 is to use reduce:
let arrs = [[1, 2], [3, 4], [5, 6]];
arrs.reduce((a, b) => [...a, ...b], []);

This is a slow solution (quadratic time).

Option 2 is that you can use Lodash:

This merges n arrays, and does it more efficiently (linear time).

Option 3 is to use concat function on array of arrays:

Which should be efficient (linear time).

Option 4 is to use .flat()

If there are more than 1 level of nested arrays you might need to use [...arrs].flat(2) and so on

let arrs = [[1, 2], [3, [7, 8], 4], [5, 6]];

Three Ways to Combine Arrays in JavaScript, Concat() The most basic way is using the concat() method. Using a 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 ellipses or spread operator. Merge Arrays With Push. 2 Ways to Merge Arrays in JavaScript Here are 2 ways to combine your arrays and return a NEW array. I like using the Spread operator. But if you need older browser support, you should use Concat.

Four ways to merge arrays in JavaScript, Learn different ways to merge an array in JavaScript. executes a reducer function (that you provide) on each element of the array, resulting in� Javascript array concat() method is used to merge two or more arrays. JavaScript arrays: copying, transferring and merging. Each parameter represents an array that is to be merged to the input array. splice function to make it work on the arguments variable. Its syntax is as follows − array.

Merge an array of size n into another array of size m+n , Merge these two arrays into the first array of size m+n such that the output is sorted. Input Duration: 2:49 Posted: Sep 5, 2009 To just merge the arrays (without removing duplicates) ES5 version use Array.concat:

Merge Arrays with JavaScript, Arrays can be merged using JavaScript's Array.prototype.push. I work with an awesome cast of developers at Mozilla, and one of them in� Definition and Usage The concat () method is used to join two or more arrays. This method does not change the existing arrays, but returns a new array, containing the values of the joined arrays.

  • What does apply do?
  • @eibersji welcome!. Also, check alternatives here