23

Is there a short way / best practice to concat 3 arrays into 1 array ?

var arr = [],
    arr1 = [ 1 , 2 , 3 ],
    arr2 = [ 4 , 5 , 6 ],
    arr3 = [ 7 , 8 , 9 ];
arr = arr.concat(arr1);
arr = arr.concat(arr2);
arr = arr.concat(arr3);
4
  • 1
    arr.concat(arr1,arr2,arr3) , w3schools Commented Feb 16, 2014 at 12:39
  • you can also use arr1+arr2+arr3 to concat Commented Feb 16, 2014 at 12:41
  • 1
    @MuhammadHaseebKhan: no, you can't do that in JavaScript. This will convert the three arrays to strings (calling .join(',') on each array) and concatenate the three strings. But, for example, you can use the plus operator in PHP to concatenate arrays. Commented Feb 16, 2014 at 12:50
  • you are right. i missed it Commented Feb 16, 2014 at 14:12

7 Answers 7

52

The shortest (and fastest) solution is arr = arr1.concat(arr2, arr3);

Alternatives:

  • arr = arr.concat(arr1, arr2, arr3)
  • arr = Array.prototype.concat(arr1, arr2, arr3)
  • arr = [].concat(arr1, arr2, arr3)
Sign up to request clarification or add additional context in comments.

2 Comments

One additional alternative arr = arr1.concat(arr2,arr3)
@TheChrisPratt I believe that's what my answer started with.
13

I would use _.flatten.

var arr = [[1,2,3], [4,5,6], [7,8,9]];
var result = _.flatten(arr) // [1,2,3,4,5,6,7,8,9]

Comments

8

Spread syntax makes concatenation easy:

arr = [...arr1, ...arr2, ...arr3]

You can even include non-array items inline:

arr = [...arr1, 42, ...arr2, ...arr3]

Comments

4

There's not much to be done. You can simplify it:

arr = arr.concat(arr1).concat(arr2).concat(arr3)

Or see @Radko Dinev answer for an even simpler (and better) way to do it.

If you have an array of arrays (with a variable number of arrays), you can try:

var m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
var new_m = [];
new_m.concat.apply(new_m, m);

Comments

3
_.reduce(arrays, function(result, arr) {
    return result.concat(arr)
}, [])

2 Comments

This solution only woks if OP is using underscore or lodash libraries. Javascript has a native Array.reduce method for this purpose.
I end up using lodash for doing map/reduce etc over dictionaries anyway, so I use it for doing these operations over arrays as well. But yeah, you could use Array methods if you don't want to use lodash.
1

A more flexible way:

var arraysToConcat = [arr1, arr2, arr3];
var arr = Array.prototype.concat.apply([], arraysToConcat);

Comments

0

If you are here in 2022, there are 3 solutions (with short syntax), a b c are arrays :

let x = [].concat(a, b, c);
let y = [...a, ...b, ...c];
let z = [a, b, c].flat();

Following benchmark :
z is the worst by far.
y is faster with small arrays, but x faster with largest arrays.
With small arrays, it's always fast.
So, we should take the fastest answer with largest arrays : x

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.