Javascript Array myEach(func)

Description

Javascript Array myEach(func)


Array.prototype.myEach = function(func) {
  for (let i = 0; i < this.length; i++) {
    func(this[i]);//from  w ww  .  j av  a 2 s  .  c o  m
  }
  return this;
};

// [1,2,3].myEach(el => console.log(el + 5));

Javascript Array myEach(func)

const NUMS = [1, 2, 3, 4, 5];//from   w ww  .java2  s.  c o  m

// Array#myEach
Array.prototype.myEach = function (func) {
  for (let i = 0; i < this.length; i++) {
    func(this[i]);
  }
};

NUMS.myEach((num) => {
  console.log(`square of ${num} is ${num * num}`);
});

// Array#.myMap
Array.prototype.myMap = function (func) {
  const mappedArray = [];

  this.myEach(element => mappedArray.push(func(element)) );

  return mappedArray;
};

console.log(NUMS.myMap( num => num * num ));

// Array#myInject
Array.prototype.myReduce = function (func, initialValue) {

  let arr = this;

  if (!initialValue) {
    initialValue = arr[0];
    arr = arr.slice(1);
  }

  let result = initialValue;

  arr.myEach(el => result = func(result, el));

  return result;
};

console.log(NUMS.myReduce( (total, item) => total + item ));



PreviousNext

Related