Nodejs Array Map map( func /*, obj */)

Here you can find the source of map( func /*, obj */)

Method Source Code

Array.prototype.map = function(  func /*, obj */) {
  var len = this.length;

  if (typeof func != 'function') {
    throw new Error("argument must be function");
  };//from   ww  w  . j  a  v a 2 s .c  o m

  var res = [];
  var obj = arguments[1];
  console.log("obj" + obj)

  for (var i = 0; i < len; i++) {
    res[i]  = func.call(obj, this[i], i, this);
  };

  return res;
}


function double(x) {
  return x*2;
}

console.log([1,2,3,4,5].map(double));

Related

  1. map( fn )
    Array.prototype.map = function( fn )
        var a = [];
        for( var i = 0; i < this.length; i++ ) {
            if( i in this ) a.push( fn( this[ i ] ) );
        return a;
    };
    
  2. map(a)
    Array.prototype.map = function(a) {
      const array = new Array(this.length)
      for (let i = 0; i < this.length; i++) array[i] = a(this[i], i, this)
      return array
    
  3. map(callback)
    Array.prototype.map = function(callback) {
      var result = [];
      for(var i = 0; i < this.length; i += 1) {
        result.push(callback.call(this[i]));
      return result;
    };
    
  4. map(callback)
    Array.prototype.map = function (callback) {
        var obj = this;
        var value, mapped_value;
        var A = new Array(obj.length);
        for (var i = 0; i < obj.length; i++) {
            value = obj[i];
            mapped_value = callback.call(null, value);
            A[i] = mapped_value;
        return A;
    };
    var arr = [1, 2, 3];
    var new_arr = arr.map(function (value) {
        return value * value;
    });
    console.log(new_arr);