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

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

Method Source Code

/**/*from  ww  w  .j  a  v a2 s . com*/
 * Invokes `fun` on each element of the array and returns a new array of the
 * results of each application.
 *
 * The first argument to `fun` is a single array
 * element and the second argument is the index of that element in the array.
 *
 * This definition is compatible with the JavaScript 1.6 definition for
 * `Array#map` in Spidermonkey and with the definition in the Prototype library.
 *
 * This implementation comes from:
 * https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/map
 *
 * @function
 * @param   {Function}  fun     function that will be applied to each array element
 * @param   {Object}    [thisp] context in which `fun` will be invoked - `this`
 * in `fun` will refer to `thisp`
 * @returns {Array} a new array made up of the return values of every invocation of `fun`
 */
Array.prototype.map = Array.prototype.map || function(fun /*, thisp*/) {
    var len = this.length >>> 0;
    if (typeof fun != "function") {
        throw new TypeError();
    }

    var res = new Array(len);
    var thisp = arguments[1];
    for (var i = 0; i < len; i++) {
        if (i in this) {
            res[i] = fun.call(thisp, this[i], i);
        }
    }

    return res;
};

Related

  1. map(fn, context)
    Array.prototype.map = function(fn, context) {
      if (typeof fn != "function") {
        throw new TypeError(fn + " is not a function");
      if (typeof context === 'undefined') {
        context = this;
      var result = new Array(this.length);
      for (var i = 0, l = this.length; i < l; ++i) {
    ...
    
  2. map(fn,thisObj)
    if(!Array.prototype.map) {
    Array.prototype.map = function(fn,thisObj)
      var scope = thisObj || window;
      var a = [];
      for(var i=0, j=this.length; i < j; ++i) {
        a.push(fn.call(scope,this[i],i,this));
      return a;
    ...
    
  3. map(from, to)
    Array.prototype.map = function(from, to) {
        var newArray = [];
        for (var i = 0; i < this.length; i++) {
            if (this[i][from] != undefined && this[i][to] != undefined) {
                newArray[this[i][from]] = this[i][to];
        return newArray;
    };
    ...
    
  4. map(fun /*, thisp*/)
    Array.prototype.map = function(fun ) {
        var len = this.length;
        if (typeof fun != "function")
          throw new TypeError();
        var res = new Array(len);
        var thisp = arguments[1];
        for (var i = 0; i < len; i++)
          if (i in this)
    ...
    
  5. map(fun /*, thisp*/)
    Array.prototype.map = function(fun ) {
        var len = this.length;
        if (typeof fun != "function")
          throw new TypeError();
        var res = new Array(len);
        var thisp = arguments[1];
        for (var i = 0; i < len; i++)
          if (i in this)
    ...
    
  6. map(func)
    Array.prototype.map = function(func){
      var result = [];
      forEach(this, function (element) {
        result.push(func(element));
      });
      return result;
    
  7. map(func)
    Array.prototype.map = Array.prototype.map || function (func) {
        array = [];
        utility.scan(this, function (item) { array.push(func(item)); });
        return array;
    };
    
  8. map(func)
    Array.prototype.map = function(func) {
      var results = [];
      for(var i = 0; i < this.length; i++) {
        var result = func(this[i]);
        results.push(result);
      return results;
    
  9. map(func)
    Array.prototype.map = function(func) {
        var length = this.length;
        var result = [];
        for ( var i = 0; i < length; i++ ) {
            result.push( func(this[i]) );
        return result;
    };