Skip to content Skip to sidebar Skip to footer

Underscore.js: _.zip.apply Example

I would like to see an example of _.zip.apply using underscore.js. In the underscore documentation is written: If you're working with a matrix of nested arrays, zip.apply can tran

Solution 1:

It's your standard use of apply:

_.zip.apply(null, [ ['foo','bar'], [0,1] ])

This would result in the following:

[['foo', 0], ['bar', 1]]

Solution 2:

You can use also a 'non-external-library' method:

Create this function:

functiontranspose(arr) {
        returnObject.keys(arr[0]).map(function (c) {
            return arr.map(function (r) {
                return r[c];
            });
        });
    }

and then:

var transposedArray = transpose(originalArray);

Post a Comment for "Underscore.js: _.zip.apply Example"