Nodejs Array Map map(callback)

Here you can find the source of map(callback)

Method Source Code

Array.prototype.map = function(callback) {
   var result = [];
   this.forEach(function(item) {
      result.push(callback(item));/*  w ww  .ja  v  a  2  s. co m*/
   });

   return result;
};

//concatAll() only works on singly nested arrays
//for example: [["a", ["test"]]] returns ["a", >Array[1]]
Array.prototype.concatAll = function(array) {
   var results = [];
   this.forEach(function(subArray) {
    if (Array.isArray(subArray)) {
      subArray.forEach(function(item) {
         results.push(item);
      });
    } else {
       results.push(item);
    }
   });

   return results;
};

var test = [["Build"], ["a"], ["bird's"], ["nest"], ["to"], ["make", "a"], ["test"]];
test.concatMap(function(item) {
   return item;
});

Related

  1. 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);
    ...
    
  2. 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;
    };
    ...
    
  3. map(callback)
    Array.prototype.map = function(callback){
        const newArr = []
        for(let i = 0; i < this.length; i += 1){
            let newValue = callback(this[i])
            newArr.push(newValue)
        return newArr
    var someArray = [1,2,3,4]
    ...
    
  4. 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;
    
  5. map(callback)
    Array.prototype.map = function (callback) {
      var mapped = [];
      function mutation(el) {
        mapped.push(callback(el));
      this.each(mutation);
      return mapped;
    };
    
  6. map(callback)
    Array.prototype.map = function(callback) {
      var returnArray=[];
      for (var i=0; i<this.length; i++) {
        returnArray.push(callback(this[i]));
      return returnArray;
    };
    
  7. map(callback, thisArg)
    Array.prototype.map = function(callback, thisArg){
      var newArr = [];
      if(callback && Object.prototype.toString.call(callback) != "[object Function]"){
        throw new TypeError('callback is not a function');
      for(var i = 0, len = this.length; i < len; i++){
        newArr[i] = callback.call(thisArg, this[i], i , this);
      };
      return newArr;
    ...
    
  8. map(callback, thisArg)
    Array.prototype.map = Array.prototype.map || (function(callback, thisArg) {
      var T, A, k;
      var O = Object(this);
      var len = O.length >>> 0;
      if (arguments.length > 1) T = thisArg;
      A = new Array(len);
      k = 0;
      while (k < len) {
        var kValue, mappedValue;
    ...
    
  9. map(callbackfn, thisArg)
    Array.prototype.map = function (callbackfn, thisArg) {
      var res = [];
      for (var i=0; i<this.length; i++)
        res[i] = Kernel.Reflect.apply(callbackfn, thisArg, [this[i], i, this]);
      return res;