This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function transpose(a) | |
{ | |
return a[0].map(function (_, c) { return a.map(function (r) { return r[c]; }); }); | |
// or in more modern dialect | |
// return a[0].map((_, c) => a.map(r => r[c])); | |
} |
Object.keys()
on the
first row of the array which yields an array of column indexes, which
we can then use map()
on to extract each column in turn.
Love to see a terser implementation if you've found one.
5 comments:
awesome :)
Godlike man...!!!
hei, i am a beginner, and i would like to know how it works? where should write in the array name?
Very nice function. Here's a terser version if you support ES6
function transpose(a)
{
return a[0].map((_, c) => a.map(r => r[c]));
}
@anon: If you're going to ES6, you might as well go all the way:
const transpose = a => a[0].map((_, c) => a.map(r => r[c])
Post a Comment