Javascript Array contains(what)

Description

Javascript Array contains(what)


Array.prototype.contains = function(what) {
  return this.indexOf(what) !== -1;
};

Javascript Array contains(what)

Array.prototype.contains = function (what) {
  var contains = false;

  //do a strict comparison
  if (typeof what !== 'function') {
    this.forEach(function (item) {
      if (contains) return;
      contains = (item === what);//w  w w .  j  a v  a  2s .  c  o m
    });
    return contains;
  }

  //compare with an evaluator
  this.forEach(function (item) {
    if (contains) return;
    contains = what(item);
  });
  return contains;
};



PreviousNext

Related