Nodejs Array Map map(projectionFunction)

Here you can find the source of map(projectionFunction)

Method Source Code

/*/*from  w  w  w. ja va2  s .c o  m*/
Exercise 4: Implement map()

To make projections easier, let's add a map() function to the Array type. Map
accepts the projection function to be applied to each item in the source array,
 and returns the projected array.
*/
Array.prototype.map = function(projectionFunction) {
   var results = [];
   this.forEach(function(itemInArray) {
      results.push(projectionFunction(itemInArray));

   });

   return results;
};

// JSON.stringify([1,2,3].map(function(x) { return x + 1; })) === '[2,3,4]'

Related

  1. map(mapper)
    Array.prototype.map = function(mapper) {
       var result = [];
       for (var i = 0; i < this.length; ++i) {
          result.push(mapper(this[i]));
       return result;
    };
    
  2. map(mappingRelation)
    Array.prototype.map = function(mappingRelation){
      return this.reduce(function(accumulator, value){
        return accumulator.concat(mappingRelation(value));
      }, []);
    
  3. map(projectionFunction)
    Array.prototype.map = function(projectionFunction) {
      var reuslts = [];
      this.forEach(function(itemInArray){
        results.push(projectionFunction(itemInArray));
      })
      return results;
    
  4. map(projectionFunction)
    Array.prototype.map = function(projectionFunction) {
      var results = [];
      this.forEach(function(itemInArray) {
        results.push(itemInArray+1);
      });
      console.log(results);
      return results;
    };
    
  5. map(projectionFunction)
    Array.prototype.map = function (projectionFunction) {
      var results = [];
      this.forEach(function (itemInArray) {
        results.push(projectionFunction(itemInArray));
      });
      return results;
    };
    
  6. map(projectionFunction)
    Array.prototype.map = function(projectionFunction) {
      var results = [];
      this.forEach(function(itemInArray) {
        results.push(projectionFunction(itemInArray));
      });
      return results;
    };
    console.log(JSON.stringify([1,2,3].map(function(x) { return x + 1; }))) 
    
  7. map(projectionFunction)
    Array.prototype.map = function(projectionFunction) {
      var results = [];
      this.forEach(function(itemInArray) {
        results.push(projectionFunction(itemInArray));
      });
      return results;
    };
    
  8. map(projectionFunction)
    Array.prototype.map = function(projectionFunction) {
      var results = [];
      this.forEach(function(itemInArray) {
        results.push(projectionFunction(itemInArray));
      });
      return results;
    };
     var answer = JSON.stringify([1,2,3].map(function(x) { return x + 1; })) === '[2,3,4]';
     console.log(answer);
    ...
    
  9. map(projectionFunction)
    Array.prototype.map = function(projectionFunction) {
      var results = [];
      this.forEach(itemInArray => results.push(projectionFunction(itemInArray)) );
      return results;
    };