Nodejs Array Zip zip()

Here you can find the source of zip()

Method Source Code

/**/*from   w ww  . j a  v  a  2  s .c  o  m*/
 * Function groups sets 2-dimensional arrays like this:
 *  [ [1,2,3], ["one","two","three"], [true,false,true] ] 
 *  => [ [1,"one",true], [2,"two",false], [3,"three",true] ] 
 */
Array.prototype.zip = function() {
    return this.map((value, index, arr) => {
        return arr.map((val, idx, array) => {
            return val[index];
        });
    });
};

// Here is how it is built step by step
// => Remember map always returns an array
// => [1], [2], [3]
// => [1, "one"], [2, "two"], [3, "three"]
// => [1, "one", true], [2, "two", false], [3, "three", true]

Related

  1. zip( fn, array )
    Array.prototype.zip = function( fn, array ) {
        var ret = [];
        for ( var i = 0; i < this.length; ++i ) {
            ret.push( fn( this[ i ], array[ i ] ) );
        return ret;
    };
    Array.cons = function( head, tail ) {
        b = [ head ];
    ...
    
  2. zip(arr, selector)
    Array.prototype.zip = function (arr, selector) {
      return this
      .take(Math.min(this.length, arr.length))
      .select(function (t, i) {
        return selector(t, arr[i]);
      });
    };
    
  3. zip(callback)
    Array.prototype.zip = function (callback) {
        callback = callback || function (data) {
            return data;
        return this.map(callback);
    var arr = [4, 9, 16, 25];
    console.log(
        arr.zip(function (data) {
    ...
    
  4. zip(callback)
    Array.prototype.zip = function (callback) {
        callback = callback || function (data) {
            return data;
        return this.map(callback);
    
  5. zip(left, right, combinerFunction)
    Array.prototype.zip = function(left, right, combinerFunction) {
      var counter;
      var results = [];
      for(counter = 0; counter < Math.min(left.length, right.length); counter++) {
        results.push(combinerFunction(left[counter],right[counter]));
      return results;
    };