Javascript Array myEach(callback)

Description

Javascript Array myEach(callback)


// function myEach(array, callback) {
//   for (let i=0; i < array.length; i++){
//     callback(array[i]);
//   }/* w ww .j a v  a  2s  .  co m*/
// }

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

function myMap(array, callback) {
  let mappedArray = [];
  for (let i=0; i < array.length; i++){
    mappedArray.push(callback(array[i]));
  }
  return mappedArray;
}

Javascript Array myEach(callback)

Array.prototype.myEach = function(callback) {
  for (let i = 0; i < this.length; i++) {
    callback(this[i]);/*ww w.ja  v a 2  s  .  co m*/
  }
};

Array.prototype.myMap = function (callback) {
  let mappedArray = [];

  function map(el) {
    mappedArray.push(callback(el));
  }

  this.myEach( map );
  return mappedArray;
};

// function plusOne(el) {
//   return el + 1;
// }
//
// console.log([1, 2, 3].myMap(plusOne));

Array.prototype.myInject = function (callback) {
  let result = this[0];
  function accumulate (el) {
    result = callback(result, el);
  }
  this.slice(1, this.length).myEach(accumulate);

  return result;
};

function multiply(a, b) {
  return a * b;
}

console.log([10,2,3].myInject(multiply));

Javascript Array myEach(callback)

Array.prototype.myEach = function (callback) {
  for (var i = 0; i < this.length; i++) {
    callback(this[i]);//from  w  w w. j  a  va 2  s .  c om
  }
  return this;
}

// var arr = [1,2,3];


// arr.myEach(function (el) {
//   console.log(el + 1);
// });

Array.prototype.myMap = function (callback) {
  var result = []
  this.myEach(function (el) {
    result.push(callback(el));
  })
  return result;
}

// console.log(arr.myMap(function (el) {
//   return el * el;
// }));

Array.prototype.inject = function (callback) {
  var accum = this[0];
  this.slice(1).myEach(function (el) {
    accum = callback(accum, el);
  });
  return accum;
};

// var arr = [1, 2, 3, 4, 5];
// console.log(arr.inject(function (accum, el){
//   return accum + el;
// }));

Javascript Array myEach(callback)

Array.prototype.myEach = function(callback) {
  for(let i = 0; i < this.length; i++) {
    callback(this[i]);//from w  w w .  ja  va 2s  . co m
  }
  return this;
}

Array.prototype.myMap = function(callback) {
  let mapped = [];
  this.myEach(el => {
    mapped.push(callback(el));
  })
  return mapped;
}

Array.prototype.myInject = function(callback) {
  let acc = this[0];
  this.slice(1).myEach(el => acc = callback(acc, el));
  return acc;
}

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

let injected = array.myInject(function(acc, el) {
  return acc + el;
})

console.log(`myInject: ${injected}`);


array.myEach(el => {
  console.log(`Logging for myEach: ${el}`);
})

let mapped = array.myMap(el => {
  return el + 1;
})

console.log(`myMap: ${mapped}`);

Javascript Array myEach(callBack)

Array.prototype.myEach = function(callBack) {
  for (let i = 0; i < this.length; i++) {
    callBack(this[i]);//from  w  ww  .j  a va 2 s  .co m
  }

  return this;
};

// [1,2,3,4,5].myEach((el) => {
//   console.log(el * 2);
// });

Javascript Array myEach(callback)

Array.prototype.myEach = function (callback) {
  for (i = 0; i < this.length; i++) {
    callback(this[i]);//from w ww  .ja  v  a2s . c om
  }
};
// let arr = new Array(1,2,3,4,5);

// let arr = [1,2,3,4,5];
//
// arr.myEach((el) => {
//   console.log(el);
// })

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

// Array.prototype.myMap = function (callback) {
//   let mapped = [];
//   this.myEach((el) => {
//     mapped.push( callback(el) );
//   })
// };
//
//
// [1,2,3,4,5].myMap((el) => { console.log(el * el) } );

Array.prototype.myInject = function (accum, callback) {
  this.myEach((el) => { accum = callback(accum, el) });
  console.log(accum);
};

[1,2,3,4,5].myInject(1, (accum, el) => {return accum * el });

Javascript Array myEach(callback)

Array.prototype.myEach = function (callback) {
  for (var i = 0; i < this.length; i++) {
    callback(this[i]);/* ww w.j  a  va  2  s . c  o  m*/
  }
  return this;
}


var a = [1, 2, 3, 4];
a.myEach(function (el) {
  console.log(el);
})

Javascript Array myEach(callback)

Array.prototype.myEach = function(callback) {
  for(let i = 0; i < this.length; i+=1){
    callback(this[i]);// ww w  .  j a  v  a2  s  .  c  o m
  }
  return this;
};

// [1,2,3,4,5].myEach((num) => {
//   console.log(`square of ${num} is ${num * num}`);
// });

Array.prototype.myMap = function(func) {
  let resultArr = [];
  this.myEach(k => resultArr.push(func(k)));
  return resultArr;
};
// let b = [1,2,3,4,5];
// let c = b.myMap(k => (k * 2));
// console.log(`new mapped array is ${c}`);
// console.log(`input array is ${b}`);


Array.prototype.myInject = function(func) {
  let accum = 0;
  this.myEach(function(el) {
    accum += func(el);
  });
  return accum;
};

console.log([1,2,3,4,29,50].myInject(function(el) {
   let sum = 0;
   sum += el;
  return sum;
}));

Javascript Array myEach(callback)

Array.prototype.myEach = function(callback) {
  for (let i = 0; i < this.length; i++) {
    callback(this[i]);//from w  w w  .ja va  2  s . c o m
  }
};

[1, 2, 3,4,5,100].myEach(function(el) {
  console.log((el + 100));
}); // => 6

Array.prototype.myMap = function(callback) {
  const newArr = [];
  // console.log(this.myEach(callback));
  this.myEach( el => newArr.push(callback(el)) );
  return newArr;
};

([1, 2, 3,4,5,100].myMap(function(el) {
  return (el + 100);
})); // => 6

Array.prototype.myReduce = function(callback, initialValue){
  const newArr = [];
  let result = initialValue;
  console.log(result);
  if (!initialValue){
    result = this[0];
    this.slice(1).myEach(function(el){result = callback(result,el);
    });
  }
  else {
    this.myEach(function(el){result = callback(result,el);
    });}
    return result;
};

console.log(NUMS.myReduce( (total, item) => total + item ));
// const NUMS = [1, 2, 3, 4, 5];



PreviousNext

Related