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);
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);
I would use
_.flatten
.There's not much to be done. You can simplify it:
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:
A more flexible way:
Spread syntax makes concatenation easy:
You can even include non-array items inline:
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)