After experimenting with the reduce method to flatten an array, I tried using a similar approach with a for-loop. Can anyone explain why the for-loop doesn't flatten the array using concat? P.S. I know that I can use a nested for-loop with Array.isArray to flatten as well. Just expected concat to work in a for-loop after seeing how reduce flattened the array.
var arrays = [
[1, 2, 3],
[4, 5],
[6]
];
console.log(arrays.reduce(function(arr, elem) {
return arr.concat(elem);
}, []));
function flatten(arr) {
var flat = [];
for (var i = 0; i < arr.length; i++) {
flat.concat(arr[i]);
}
return flat;
}
console.log(flatten(arrays));