- Когда cols = 3, rows = 3
Ввод:
3 | 4 | 5
6
Ожидается:
0 | 3 | 5
1 | 4 | 6
2 |
- Когда столбцы = 5, строки = 2
0 | 1 | 2 | 3 | 4
5 | 6
Ожидается:
0 | 2 | 4 | 5 | 6
1 | 3
ОБНОВЛЕНИЕ с кодом JavaScript
const input = [[0, 1, 2], [3, 4, 5], [6]];
const expected = convert(input);
// expected = [[0, 3, 5], [1, 4, 6], [2]];
const input = [[0, 1, 2, 3, 4], [5, 6]];
const expected = convert(input);
// expected = [[0, 2, 4, 5, 6], [1, 3]];
ОБНОВЛЕНИЕ 2
Я пробовал так:
const input = [[0,1,2],
[3,4,5],
[6]];
const array = flatToArray(input);
console.log("--- Array:---");
console.log(array);
const rows = input.length;
const cols = input[0].length;
const result = [];
for (let i = 0; i < rows; i++) {
result = [];
for (let j = 0; j < cols; j++) {
result.push(array[i%rows + j*rows]);
}
}
function flatToArray(input) {
return input.reduce((prev, current) => prev.concat(current), []);
}
console.log("--- Final:---");
console.log(result)
Выход:
--- Array:---
[
0, 1, 2, 3,
4, 5, 6
]
--- Final:---
[ [ 0, 3, 6 ], [ 1, 4, undefined ], [ 2, 5, undefined ] ]
Подробнее здесь: https://stackoverflow.com/questions/792 ... -to-bottom