Nodejs Array Map mapByTwo(fun /*, thisp*/)

Here you can find the source of mapByTwo(fun /*, thisp*/)

Method Source Code

Array.prototype.mapByTwo = function(fun /*, thisp*/) {
  if (typeof fun != "function")
    throw new TypeError();/*from w w w. ja v a 2 s .c  o  m*/

  var len = this.length >>> 0;
  var thisp = arguments[1];
  var k = 0;
  var A = new Array(Math.ceil(len/2));

  if (len/2 < A.length) {
    this.push(null);
  }

  for (var i = 0; i < len; i++) {
    if (i in this) {
      A[k] = fun.call(thisp, this[i], this[++i], --i, ++i, this);
    }
    k++;
  }

  return A;

};

Related

  1. map(projectionFunction)
    Array.prototype.map = function(projectionFunction) {
      var results = [];
      this.forEach(function(itemInArray) {
      });
      return results;
    };
    JSON.stringify(
      [1,2,3].map(function(x) {
        return x + 1;
    ...
    
  2. map(testFunction)
    Array.prototype.map = function(testFunction){
      var result = [];
      this.forEach(function(item){
        result.push(testFunction(item));
      })
      return result;
    
  3. map(transform)
    Array.prototype.map = function (transform) {
      let mappedArray = [];
      const len = this.length;
      for (let i = 0; i < len; i += 1) {
        mappedArray.push(transform(this[i], i, this));
      return mappedArray;
    };
    
  4. map(transformer)
    Array.prototype.map = function(transformer) {
      var result = []
      for ( var i = 0; i < this.length; i++) {
        result.push(transformer(this[i]));
      return result;
    
  5. map2(projection)
    const stocks= [
      {symbol: 'XFX', price: 240.22, volume: 23432},
      {symbol: 'TNZ', price: 332.19, volume: 234},
      {symbol: 'JXJ', price: 120.22, volume: 5323},
    ]
    Array.prototype.map2= function(projection) {
      let results= []
      this.forEach(item => results.push(projection(item)))
      return results
    ...
    
  6. mapMe(func)
    Array.prototype.mapMe = function (func) {
      var arr = this, result = [];
      for (var i = 0; i < arr.length; i++) {
        result.push(func(arr[i]));
      return result;
    };
    
  7. mapValues(value)
    Array.prototype.mapValues = function(value) { 
        return this.map(function(e) { 
            return e[value] 
        })
    
  8. mapp(callback)
    Array.prototype.mapp = function(callback) {
      var newArray = [];
      for(var i = 0; i < this.length; i++){
        newArray.push(callback(this[i]));
      return newArray;
    var x = [1,2,3];
    x.mapp(function(ele) {
    ...
    
  9. mapper(callback)
    Array.prototype.mapper = function(callback){
        var arr = [];
        for (var i = 0; i < this.length; i++)
            callback(this[i]);
        return arr;
    };
    var arr = [1, 2, 3];
    Array.prototype.mapper = function(callback){
        for (var i = 0; i < this.length; i++)
    ...