JS to determine whether an element exists in a JS array, equivalent to the PHP language in the In_array function.
Array.prototype.s=string.fromcharcode (2);
Array.prototype.in_array=function (e) {
var r=new RegExp (this. S+e+this. S);
Return (R.test) (this. S+this.join (this. S) +this. S));
};
Usage is as follows:
var arr=new Array (["B", 2, "a", 4, "Test"]);
Arr.in_array (' test ')//Determines whether the test string exists in the ARR array, returns true otherwise false, and returns true here
Note: This function is valid only for characters and numbers
There are similar functions in jquery: Http://docs.jquery.com/Utilities/jQuery.inArray
Its code is as follows:
function InArray (needle, haystack) {
var length = haystack.length;
for (var i = 0; i < length; i++) {
if (haystack[i] = = needle) return true;
}
return false;
}
The above is the article to share all the content, I hope you can enjoy.