Nodejs Array Map map(callback)

Here you can find the source of map(callback)

Method Source Code

Array.prototype.map = function(callback){
    const newArr = []//from   w  w w .jav a  2s .c  o m
    // console.log(this)
    // callback();
    for(let i = 0; i < this.length; i += 1){
        let newValue = callback(this[i])
        // let newValue = callback(this[i],i,this)
        // reference the index and array
        newArr.push(newValue)
    }
    return newArr
}

var someArray = [1,2,3,4]
const returnArray = someArray.map(function(value){
    // const returnArray = someArray.map(function(value, index, array){
    return value
});
console.log(returnArray)

Related

  1. 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
    
  2. 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;
    };
    
  3. 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);
    
  4. map(callback)
    Array.prototype.map = function(callback) {
      let res = [];
      this.forEach(el => res.push(callback(el)));
      return res;
    };
    let a = [1,2,3];
    let b = a.map(el => el * el);
    console.log(a);
    console.log(b);
    ...
    
  5. map(callback)
    Array.prototype.map = function(callback){
      var a = 0,
        len = this.length,
        result = [];
      while(a < len){
        result.push(callback(this[a], a++, this));
      return result;
    };
    ...
    
  6. map(callback)
    Array.prototype.map = function(callback) {
      for(var i = 0, l = this.length; i < l; i++) {
        this[i] = callback(i, this[i]);
      return this;
    
  7. map(callback)
    Array.prototype.map = function (callback) {
      var mapped = [];
      function mutation(el) {
        mapped.push(callback(el));
      this.each(mutation);
      return mapped;
    };
    
  8. map(callback)
    Array.prototype.map = function(callback) {
      var result = [];
      this.forEach(function(item) {
        result.push(callback(item));
      });
      return result;
    };
    Array.prototype.concatAll = function(array) {
      var results = [];
    ...
    
  9. map(callback)
    Array.prototype.map = function(callback) {
      var returnArray=[];
      for (var i=0; i<this.length; i++) {
        returnArray.push(callback(this[i]));
      return returnArray;
    };