Below is an array of subarrays
const array = [
[0, 1, 2, 3], // Going from top to bottom?
[4, 5, 6, 7, 8, 9], // |
[10, 11, 12, 13, 14], // |
[15, 16, 17, 18, 19], // |
] // V
My goal is to achieve the following output
newArray = [0, 4, 10, 15, 1, 5, 11, 16, 2, 6, 12 ...]
This is my current approach:
let idx = 0;
let newArray = []
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array[i].length; j++) {
newArray.push(array[idx][j])
idx++;
}
idx = 0;
}