console.table icon indicating copy to clipboard operation
console.table copied to clipboard

Enhancement : rotate/pivet table

Open WORMSS opened this issue 9 years ago • 2 comments

I was wondering if it would be possible to list the table in the other way,

console.table( [["a", "b", "c", "d"],["e", "f", "g", "h"]] );

current comes out

0  1  2  3
-  -  -  -
a  b  c  d
e  f  g  h

would come out

0 | a  e
1 | b  f
2 | c  g
3 | d  h

This has come about because I have few tables with lots of data, rather than lots of tables with few data. I know with the table layout above it doesn't seem that needed. But when each one of those arrays holds 43 data, but there is only 4 lots of them, it gets hard to use.

image

This is a screenshot of 2 arrays with 43.

  • Colin

WORMSS avatar Feb 04 '16 13:02 WORMSS

This looks like a good use case for a pre-process, hmm, or it could be detected automatically. Feel free to implement, I would be happy to merge

bahmutov avatar Feb 04 '16 15:02 bahmutov

I have made a very rudimentary pivot function but it is by no means up to a standard to use for everything else.

There are prob much more efficient ways to do this, and this one ONLY deals with a 2 dimensional array, not objects.

function pivetData(array) {
    var columns = array.length;
    var rows = array.reduce(function (a, item) {
        return Math.max(a, item.length);
    }, 0);

    var returnArray = [];
    for ( var i = 0; i < rows; i++ ) {
        var a = [];
        for ( var j = 0; j < columns; j++ ) {
            a.push(array[j][i]);
        }
        returnArray.push(a);
    }
    return returnArray;
};

But it worked enough for my simple needs currently (it will get more complicated before the end). But this now displays twice as much data as my last screen shot (4 sets rather than just the 2), but much nicer.

image

WORMSS avatar Feb 04 '16 15:02 WORMSS