Development often use this same logic, to determine whether a string or number in the array, many programming languages have such a special function, such as often used in PHP has a in_array (), but the JS incredibly wood there, wood you know?
Baidu also has a lot of ways to achieve, here to leave a kind of their own commonly used!
Attached code:
function In_array (Stringtosearch, Arraytosearch) {for
(s = 0; s < arraytosearch.length; s++) {
thi SEntry = Arraytosearch[s].tostring ();
if (thisentry = = Stringtosearch) {return
true;
return
false;
}
Usage:
var abc=[1,2,3,4];
var z = 4;
if (In_array (Z,ABC)) {
Console.log (' Congratulations, including ' +z ');
} else{
console.log (' Sorry, not including ' +z ');
}
This article by the shadow shallow ' s Blog in July 2015, published in the front-end development category, you can comment, and retain the original address and the author of the case to refer to your website or blog