We often use this same logic to determine whether a string or number is in an array, and many programming languages have such a special function, such as PHP's In_array (). Then JS has wood there, unfortunately, JS Wood has such a function, so think of the great JQ whether encapsulated this function, found the API,JQ does encapsulate this function
Jquery.inarray (value, array) searches the array for the specified value and returns its index (1 if it is not found).
Value to search for.
An array of arrays through which to search.
Of course, in learning, I also write such a function:
Copy Code code as follows:
function InArray1 (needle,array,bool) {
if (typeof needle== "string" | | typeof needle== "number") {
for (var i in array) {
if (Needle===array[i]) {
if (bool) {
return i;
}
return true;
}
}
return false;
}
}
Three parameters, find Needle,bool in array as Boolean, and return needle position in array if true