JSFiddle - React, Tailwind, and code Playground
JavaScript
var input = [[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]];
function run(input, result) {
if (input.length == 0) {
return result;
}
result = result.concat(input.shift());
input.forEach(function(rightEnd) {
result.push(rightEnd.pop());
});
result = result.concat(input.pop().reverse());
var tmp = [];
input.forEach(function(leftEnd) {
tmp.push(leftEnd.shift());
});
result = result.concat(tmp.reverse());
return run(input, result);
}
var result = run(input, []);
console.log('result', result);