Using es6 spread to concat multiple arrays

JavascriptEcmascript 6

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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).

Solution 2 - Javascript

Another option could be:

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

Solution 3 - Javascript

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

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


console.log(produce);

Solution 4 - Javascript

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);

Solution 5 - Javascript

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);

Solution 6 - Javascript

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);

Solution 7 - Javascript

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...

Solution 8 - Javascript

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]

Solution 9 - Javascript

We can resolve using es6 following way

function mergeTwo(arr1, arr2) {
  let result = [...arr1, ...arr2];
  return result.sort((a,b) => a-b);
}

Solution 10 - Javascript

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

console.log(arrs);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondanday74View Question on Stackoverflow
Solution 1 - JavascriptBrian McCutchonView Answer on Stackoverflow
Solution 2 - JavascriptacontellView Answer on Stackoverflow
Solution 3 - JavascriptAJEET SINGHView Answer on Stackoverflow
Solution 4 - JavascriptPankaj ShrivastavaView Answer on Stackoverflow
Solution 5 - JavascriptPedro CastilhoView Answer on Stackoverflow
Solution 6 - Javascriptguest271314View Answer on Stackoverflow
Solution 7 - Javascriptajaykumar mpView Answer on Stackoverflow
Solution 8 - JavascriptMulanView Answer on Stackoverflow
Solution 9 - JavascriptKARTHIKEYAN.AView Answer on Stackoverflow
Solution 10 - Javascriptdanday74View Answer on Stackoverflow