66

We all know you can do:

let arr1 = [1,2,3];
let arr2 = [3,4,5];
let arr3 = [...arr1, ...arr2]; // [1,2,3,3,4,5]

But how do you make this dynamic to concat N arrays?

Brian McCutchon
  • 8,354
  • 3
  • 33
  • 45
danday74
  • 52,471
  • 49
  • 232
  • 283

10 Answers10

84

One option is to use reduce:

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

Of course, this is a slow solution (quadratic time). Alternatively, if you can use Lodash, _.flatten does exactly what you want, and does it more efficiently (linear time).

EDIT

Or, adapted from Xotic750's comment below,

[].concat(...arrs);

Which should be efficient (linear time).

Brian McCutchon
  • 8,354
  • 3
  • 33
  • 45
  • 3
    One you have your arrays in an array, you could then. `Array.prototype.concat.apply([], arr)` Though this isn't using the spread operator any more. – Xotic750 Apr 17 '17 at 17:14
  • 1
    @Xotic750 Nice, but you can do that using the spread operator! See my edit. – Brian McCutchon Apr 17 '17 at 17:18
  • 1
    Note that in typescript you need to force the empty array to be typed if you want the return value to be `number[]` and not `any[]` (eg. `([] as number[]).concat(...arrs)`) – Simon_Weaver Aug 09 '19 at 04:42
22

Another option could be:

const nArrays = [
  [1, 2, 3, 4, 5],
  [6, 7, 8, 9],
  [10, 11]
];
const flattened = [].concat(...nArrays);
console.log(flattened)
acontell
  • 6,792
  • 1
  • 19
  • 32
17

let fruits = ["apples", "bananas", "pears"];
let vegetables = ["corn", "potatoes", "carrots"];

let produce = [...fruits, ...vegetables];


console.log(produce);
AJEET SINGH
  • 431
  • 5
  • 17
  • 14
    This given code is exactly same as the given question. It is not an answer for making dynamiclly concat solution for N arrays. – yue you Feb 05 '18 at 00:32
4

Best option is to use FlatMap, which helps us to conact multiple arrays into one single array.

Example:

let arrs = [[1, 2], [3, 4], [5, 6]];
arrs.flatMap(a => a);

result will be

> (6) [1, 2, 3, 4, 5, 6]

Happy Coding...

ajaykumar mp
  • 487
  • 5
  • 12
3

Following solution works for me (spread operator in ES6):

let array = ['my','solution','works'];
let newArray = [];
let newArray2 = [];
newArray.push(...array); //adding to same array
newArray2.push([...array]); //adding as child/leaf/sub-array
console.log(newArray);
console.log(newArray2);
2

You can't do that with spread syntax alone, as spread syntax requires you to know how many arrays you are concatenating in advance. However, you could write the following function:

function concatN(...arguments) {
    let accumulator = [];
    for(let arg = 0; arg < arguments.length; arg = arg + 1) {
        accumulator = [...accumulator, ...arguments[arg]];
    }
    return accumulator;
}

It probably won't be very efficient, though (repeated use of spread syntax is O(n²)). Using Array.prototype.concatwould be better. You can just do:

[].concat(all, of, your, arrays);
Pedro Castilho
  • 10,174
  • 2
  • 28
  • 39
2

You can use spread element within for..of loop to concatenate array values to a single array

let arr1 = [1,2,3];
let arr2 = [3,4,5];
let arr3 = [];

for (let arr of [arr1, arr2 /* , arrN */]) arr3.push(...arr);

console.log(arr3);
guest271314
  • 1
  • 15
  • 104
  • 177
1

You could use a recursive function and Array.prototype.concat

const concatN = (x,...xs) =>
  x === undefined ? [] : x.concat(concatN(...xs))

console.log(concatN([1,2,3], [4,5,6], [7,8,9]))
// [1,2,3,4,5,6,7,8,9]

You can do the same thing using reduce and Array.prototype.concat. This is similar to the accepted answer but doesn't senselessly use spread syntax where x.concat(y) is perfectly acceptable (and likely heaps faster) in this case

const concatN = (...xs) =>
  xs.reduce((x,y) => x.concat(y), [])

console.log(concatN([1,2,3], [4,5,6], [7,8,9]))
// [1,2,3,4,5,6,7,8,9]
Mulan
  • 129,518
  • 31
  • 228
  • 259
1

We can resolve using es6 following way

function mergeTwo(arr1, arr2) {
  let result = [...arr1, ...arr2];
  return result.sort((a,b) => a-b);
}
KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
0

let arr1 = [1,2,3];
let arr2 = [3,4,5];
let arrs = [arr1, arr2].flat(); // [1,2,3,3,4,5]

console.log(arrs);
danday74
  • 52,471
  • 49
  • 232
  • 283