Possible Duplicate:
array.contains(obj) in JavaScript
Let's say I have an array = [0,8,5]
What is the fastest way to know if 8 is inside this one.. for example:
if(array.contain(8)){
// return true
}
I found this : Fastest way to check if a value exist in a list (Python)
and this : fastest way to detect if a value is in a set of values in Javascript
But this don't answer to my question. Thank you.
Use indexOf()
to check whether value is exist or not
array.indexOf(8)
Sample Code,
var arr = [0,8,5];
alert(arr.indexOf(8)); //returns key
For IE support
//IE support
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(obj, start) {
for (var i = (start || 0), j = this.length; i < j; i++) {
if (this[i] === obj) { return i; }
}
return -1;
}
}
var arr = [0,8,5];
alert(arr.indexOf(8))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With