Javascript: inArray
Written by Administrator   
luned́, 25 agosto 2008

Here below a simple example to build a inArray Javascript method simil to PHP function.
This solution should work fairly well on relatively small lists of items.

Array.prototype.inArray = function (value) {
  var i;
  for (i=0; i < this.length; i++) {
    if (this[i] === value) {
    return true;
    }
  }
  return false;
};

The method searches for an identical value, rather than a similar one. If you want just a similar value (the string "003" should match for example with the integer 3), this could be changed by replacing "===" with "==" in line 4.
To use the function have a look at the below example:

if (MyArray.inArray('value')) {
  alert('value found');
}

Last Updated ( luned́, 25 agosto 2008 )