Javascript simulates the php function in_array, javascriptin_array
Js determines whether an element exists in a js array, which is equivalent to the in_array function in php.
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 ));};
The 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. If yes, true is returned. Otherwise, false is returned. true is returned here.
Note: This function is only valid for characters and numbers.
JQuery has a similar function: http://docs.jquery.com/Utilities/jQuery.inArray
The 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 all the content shared in this article. I hope you will like it.