Nodejs Array Map map(projectionFunction)

Here you can find the source of map(projectionFunction)

Method Source Code

//http://reactivex.io/learnrx/
//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));
   });// w  w w.  ja  v  a  2s.c  o  m
   return results;
};

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

Related

  1. map(mappingRelation)
    Array.prototype.map = function(mappingRelation){
      return this.reduce(function(accumulator, value){
        return accumulator.concat(mappingRelation(value));
      }, []);
    
  2. map(projectionFunction)
    Array.prototype.map = function(projectionFunction) {
      var reuslts = [];
      this.forEach(function(itemInArray){
        results.push(projectionFunction(itemInArray));
      })
      return results;
    
  3. map(projectionFunction)
    Array.prototype.map = function(projectionFunction) {
      var results = [];
      this.forEach(function(itemInArray) {
        results.push(itemInArray+1);
      });
      console.log(results);
      return results;
    };
    
  4. map(projectionFunction)
    Array.prototype.map = function (projectionFunction) {
      var results = [];
      this.forEach(function (itemInArray) {
        results.push(projectionFunction(itemInArray));
      });
      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;
    };
    
  7. 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);
    ...
    
  8. map(projectionFunction)
    Array.prototype.map = function(projectionFunction) {
      var results = [];
      this.forEach(itemInArray => results.push(projectionFunction(itemInArray)) );
      return results;
    };
    
  9. map(projectionFunction)
    Array.prototype.map = function (projectionFunction) {
      var results = [];
      this.forEach(function (itemInArray) {
        results.push(projectionFunction(itemInArray));
      });
      return results;
    };