Javascript Array mySelect(cb)

Description

Javascript Array mySelect(cb)


Array.prototype.mySelect = function (cb) {
  const result = [];//  w w w . j  av  a 2  s .  c  o  m
  this.forEach(function(ele){
    if (cb(ele)){
      result.push(ele);
    }
  });
  return result;
};

//tester callback
function lessThan10(num){
  return num < 10;
}

console.log([1,2,3,4,12,13,14].mySelect(lessThan10));



PreviousNext

Related