JS: Flatten Array of Arrays Using Reduce
function flatten(arr){
return arr.reduce(function(prev, current) {
return prev.concat(current);
}, []); // initalize value with []
}
var arr_of_arrays = [[0, 1], [2, 3], [4, 5]];
console.log(flatten(arr_of_arrays)); // [0, 1, 2, 3, 4, 5]
Another way to flatten array is using concat
with apply
:
var arrays = [[0, 1], [2, 3], [4, 5]];
var merged = [].concat.apply([], arrays);
In above code, var merged
is equivalent to:[].concat([0, 1], [2, 3], [4, 5])
.
The
apply()
method calls a function with a given this value and arguments provided as an array