Nodejs Array Zip zip(callback)

Here you can find the source of zip(callback)

Method Source Code

//Return an array in javascript
Array.prototype.zip = function (callback) {
    callback = callback || function (data) {
        return data;
    }//from  w w w  .j  a  va2  s  .c o  m
    return this.map(callback);
}
//This would allow us to the following:
var arr = [4, 9, 16, 25];
console.log(
    arr.zip(function (data) {
        return Math.sqrt(data, 2);
    }).map(function (n) {
        return n * 2
    })
);

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()
    Array.prototype.zip = function() {
        return this.map((value, index, arr) => {
            return arr.map((val, idx, array) => {
                return val[index];
            });
        });
    };
    
  3. 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]);
      });
    };
    
  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;
    };
    
  6. zip(xs, ys, modify)
    Array.zip = function(xs, ys, modify) {
        const result = [];
        for(let i = 0; i < Math.min(xs.length, ys.length); i++) {
            result.push(modify(xs[i], ys[i]));
        return result;