Javascript Array contain(arg)

Description

Javascript Array contain(arg)


Array.prototype.contain = function(arg){
 var i=0;/*  w w w . ja  va2s  .co m*/
 var arrSize = this.length;
 for(i=0;i<arrSize;i++){
  if(this[i] == arg){
   return true;
  }
 }
 return false;
};

Javascript Array contains( needle )


/**/*  w w  w. java 2 s  . co  m*/
 * Array.prototype.[method name] allows you to define/overwrite an objects method
 * needle is the item you are searching for
 * this is a special variable that refers to "this" instance of an Array.
 * returns true if needle is in the array, and false otherwise
 */
Array.prototype.contains = function ( needle ) {
   for (i in this) {
       if (this[i] == needle) return true;
   }
   return false;
}

// Now you can do things like:
var x = Array();
if (x.contains('foo')) {
   // do something special
}



PreviousNext

Related