i want a function for javascript like in_array in PHP to look for the special characters in an array. if that special character is in the array it returns true.
-
possible duplicate of JavaScript is in arrayFelix Kling– Felix Kling2011年10月14日 06:58:41 +00:00Commented Oct 14, 2011 at 6:58
3 Answers 3
There is a javascript function indexOf
var myArray = [9, 3, 4, 7];
var index = myArray.indexOf(4);
// index is 2
index = myArray.indexOf(6);
// index is -1
2 Comments
If you use jQuery, maybe this plugin will help:
Comments
You can resort to indexOf if you (the client you are serving) have javascript 1.6
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
On the same page there is also an equivalent function if you don't have the function implemented natively into the engine.
For further references on array methods you can read
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array