I have a problem that asks me to join arrays of an array and return a single array in the form of [ array[0][0], array[0][1], array[1][0], array[1][1], etc. ]
. I solved it using the push
method in nested for-loops, but the prompt says that I should be familiar with the concat
method. I know the concat
method syntax and how it works, but I can't figure out how to use it to do what the prompt asks for.
Here's my solution using the push
method:
function joinArrayOfArrays(arr) {
var joined = [];
for (var i = 0; i < arr.length; i++) {
for (var k = 0; k < arr[i].length; k++) {
joined.push(arr[i][k]);
}
}
return joined;
}
joinArrayOfArrays([[1, 4], [true, false], ['x', 'y']]);
// => [ 1, 4, true, false, 'x', 'y' ]
How would I return the same output using the concat
method?