Javascript Array contains(element)

Description

Javascript Array contains(element)



/**/*from   w  ww.  ja va  2s  .c o m*/
 * Checks whether the array contains a given element.
 *
 * @method Array.prototype.contains
 * @param {Any} element The element to search for
 * @return {Boolean} True if the array contains a given element
 */
Array.prototype.contains = function(element) {
    return this.indexOf(element) >= 0;
};

Javascript Array contains(element)

Array.prototype.contains = function(element) {
    var i = this.length;
    while(i--) {/* w ww. j a v  a2s  .com*/
        if (_.isEqual(this[i], element)) {
            return true;
        }
    }
    return false;
}

Javascript Array contains(element)

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

Javascript Array contains(element)

Array.prototype.contains = function(element) {
  return this.indexOf(element) >= 0;
}

Object.prototype.keys = function() {
  return Object.keys(this);
}

Javascript Array contains(element)

//javascript the good parts taught me this!

Array.prototype.contains = function (element) 
  {//from www .jav a2 s  .  co  m
          for (var i = 0; i < this.length; i++) 
       {
              if (this[i] == element) 
          {
                      return true;
              }
          }
          return false;
  };

Javascript Array contains(element)

Array.prototype.contains = function (element) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == element) {
            return true;
        }/*from  w  ww  .  ja v  a2  s  .c o  m*/
    }
    return false;
}

Javascript Array contains(element)

Array.prototype.contains = function (element) {
 var found = false;
 this.forEach(function (item) {
  if (element === item) {
   found = true;//from w  w w .  j  av  a  2s  . c o m
  }
 });
 return found;
};

var a = [1, 2, 3];

console.log(a.contains(1));
//console.log(Array.prototype.contains.apply(a, [1]));

Javascript Array contains(element)

Array.prototype.contains = function(element) {
    var i = this.length;
    while (i--) {
        if (this[i] === element) {
            return true;
        }/*w  w w  .j  a  v a2 s .  c o  m*/
    }
    return false;
};

Array.prototype.clear = function() { this.length = 0; };


// Divides an array in several arrays of a given maximum size
/* Example
    Input :chunk([1,2,3,4,5,6,7], 3)
    Output: [[1,2,3],[4,5,6],[7]]
*/
function chunk(a, s){
    for(var x, i = 0, c = -1, l = a.length, n = []; i < l; i++)
        (x = i % s) ? n[c][x] = a[i] : n[++c] = [a[i]];
    return n;
}

Javascript Array contains(element)

Array.prototype.contains = function(element) { // the in operator does not work as expected for some reason ...
    for(var i = 0; i < this.length; i ++)
        if(this[i] === element) return true;
    return false;
};

Javascript Array contains(element)

/**/* w w  w  .  j av a 2  s. com*/
 * Array.js
 *
 * Extends the Array object.
 *
 * @author Maxime Ollagnier
 */
/**
 * Check if the given element exists in this array.
 */
Array.prototype.contains = function(element)
{
    for (var i = 0; i < this.length; i++) 
    {
        if (this[i] == element) 
        {
            return true;
        }
    }
    return false;
};



PreviousNext

Related