Javascript Array any(cb)

Description

Javascript Array any(cb)


// [1, 2, 3, 4, 5].any(isGreaterThanZero); //=> true
// [-1, 0].any(isGreaterThanZero); //=> false 

Array.prototype.any = function(cb) {
  for(let i = 0; i < this.length; i++){
    if(cb(this)) return true;
  }//from ww w .  j  a  va2 s  . co  m
  return false;
};

function isGreaterThanZero(array) {
  for(let i = 0; i < array.length; i++){
    if(array[i] < 0){
      return false;
    }
  }
  return true;
}

console.log([1, 2, 3, 4, 5].any(isGreaterThanZero));
console.log([-1, 0].any(isGreaterThanZero));

Javascript Array any(delegate)


Array.prototype.any = function(delegate) {    
  for (var i=0; i < this.length; i++) {      
    if (delegate.apply(this, [this[i]]) === true) {
      return true;
    }//from   w ww  .  j  av  a2  s.  c  o  m
  }      
  return false;  
};

Javascript Array any(fn)

Array.prototype.any = function (fn) {
  return this.filter(fn).length > 0
}

Javascript Array any(itemTest)


Array.prototype.any = function (itemTest) {
 for (var i = 0; i < this.length; i++)
  if (itemTest(this[i]))
   return true;// w w w .  ja  v  a 2 s  .  c  o m
 return false;
}

Javascript Array any(p)

Array.prototype.any = function (p) {
  return this.filter(p).length > 0;
};

Javascript Array any(p)

Array.prototype.any = function (p) {
  return this.reduce( (result, current) =>
    result ? result : p(current), false);
};

Javascript Array any(p)

Array.prototype.any = function (p) {
  if (this.length === 0) {
    return false;
  }//from   www  .  jav  a  2s  .  com
  
  return this.filter(p).length > 0;
};

Javascript Array any(p)

Array.prototype.any = function (p) {

  for (var i = 0; i < this.length; i++) {
   if(p(this[i]) == true){
    return true;//from   w w w.jav  a 2s.c  om
   }
  };
  return false;
};

Javascript Array any(p)

Array.prototype.any = function (p) {
  for(var i = 0; i < this.length; i++){
    if(p(this[i])) return true;
  }//from w w w .j  av  a 2  s  .  c  om
  return false;
};
function isGreaterThanZero (num) {
  return num > 0;
}

function isLessThanZero (num) {
  return num < 0;
}

console.log([-1, 2, 3].any(isGreaterThanZero)); // true
console.log([-1, -2, -3].any(isGreaterThanZero)); // false

Javascript Array any(predicate)

Array.prototype.any = function(predicate){
  for (var i = 0; i < this.length; i++){
    if (predicate(this[i])) {
      return true
    }//from   w  ww . j  ava  2  s . c o  m
  }
  return false
}

Javascript Array any(predicate)

Array.prototype.any = function (predicate) {
 if (this.length == 0) return false;
 return this.first(predicate) !== undefined;
};



PreviousNext

Related