Javascript Array myMap(cb)

Description

Javascript Array myMap(cb)


Array.prototype.myMap = function(cb){
    let res = [];
    this.forEach( el => {//w ww  .  ja  v  a 2  s  .c o  m
        res.push(cb(el));
    });
    return res;
};

let arr = [1,2,3,4,5];

let brr = arr.myMap(el => el * 2);

console.log(brr);

Javascript Array myMap(cb)

Array.prototype.myMap = function (cb) {

  let map = [];//from   w w w .  ja va  2 s. com

  this.myEach((el) => {
    map.push(cb(el));
  });

  return map;
};

let myMapArray = [1, 2, 3];
// console.log(myMapArray.myMap(el =>  el * 2));



PreviousNext

Related