Nodejs Array Map map(projectionFunction)

Here you can find the source of map(projectionFunction)

Method Source Code

// 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) {

      // ------------ INSERT CODE HERE! ----------------------------
      // Apply the projectionFunction to each item in the array and add
      // each result to the results array.
      // Note: you can add items to an array with the push() method.
      // ------------ INSERT CODE HERE! ----------------------------

   });/*  www .ja v a  2  s.  c o m*/

   return results;
};

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

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

Related

  1. 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; }))) 
    
  2. map(projectionFunction)
    Array.prototype.map = function(projectionFunction) {
      var results = [];
      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(projectionFunction(itemInArray));
      });
      return results;
    };
     var answer = JSON.stringify([1,2,3].map(function(x) { return x + 1; })) === '[2,3,4]';
     console.log(answer);
    ...
    
  4. map(projectionFunction)
    Array.prototype.map = function(projectionFunction) {
      var results = [];
      this.forEach(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(testFunction)
    Array.prototype.map = function(testFunction){
      var result = [];
      this.forEach(function(item){
        result.push(testFunction(item));
      })
      return result;
    
  7. 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;
    };
    
  8. map(transformer)
    Array.prototype.map = function(transformer) {
      var result = []
      for ( var i = 0; i < this.length; i++) {
        result.push(transformer(this[i]));
      return result;
    
  9. 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
    ...