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)]
1

3 Answers 3

4

You can use ES6's spread:

var arrays = [[1, 2], [3, 4], [5, 6]];
var res = [].concat(...arrays);
console.log(res);

Sign up to request clarification or add additional context in comments.

2 Comments

Good thought making use of concat's handling of multiple arguments.
@Bikshu s (and lurkers): If you can't use spread, the above is effectively: var res = Array.prototype.concat.apply([], arrays);
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 `[]`

3 Comments

Producing unnecessary intermediate arrays along the way... :-)
@T.J.Crowder unnecessary intermediate arrays True, Or [].concat.apply([], arr) would suffice
Or you could avoid that last unnecessary array like this.
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);
});

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.