1

How to concat all this array into a single array:

[Array(10), Array(10), Array(10), Array(10), Array(10), Array(10), Array(10), Array(2)]
yajiv
  • 2,901
  • 2
  • 15
  • 25
Bikshu s
  • 389
  • 4
  • 14
  • Possible duplicate of [Merge/flatten an array of arrays in JavaScript?](https://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays-in-javascript) – Mohammad Usman Mar 02 '18 at 10:02

3 Answers3

4

Use reduce and concat

var output = arr.reduce( (a, c) => a.concat(c), []); //assuming arr is the input array

Edit

As @TJ mentioned in his comment, that above solution will create some intermediate arrays along the way, you can try (concat without spread)

var output = [].concat.apply([], arr);

or

var output = Array.prototype.concat.apply([], arr); //avoiding usage of another unnecessary array `[]`
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • 1
    Producing unnecessary intermediate arrays along the way... :-) – T.J. Crowder Mar 02 '18 at 09:36
  • @T.J.Crowder *unnecessary intermediate arrays* True, Or `[].concat.apply([], arr)` would suffice – gurvinder372 Mar 02 '18 at 09:41
  • Or you could avoid that last unnecessary array [like this](https://stackoverflow.com/questions/49066319/how-to-concat-multiple-array-into-single-array/49066344?noredirect=1#comment85137775_49066381). – T.J. Crowder Mar 02 '18 at 09:46
4

You can use ES6's spread:

var arrays = [[1, 2], [3, 4], [5, 6]];
var res = [].concat(...arrays);
console.log(res);
Faly
  • 13,291
  • 2
  • 19
  • 37
0
var array1 = ['a', 'b', 'c'];
var array2 = ['d', 'e', 'f'];

console.log(array1.concat(array2));
// expected output: Array ["a", "b", "c", "d", "e", "f"]

If you have an array of array you can do like so :

let bigArray = new Array();

arrayOfArray.forEach((arr) => {
    bigArray.concat(arr);
});
Jeremy M.
  • 1,154
  • 1
  • 9
  • 29