We often use this logic to determine whether a string or number is in an array. Many programming languages have such special functions, such as PHP's in_array(). So is there any JS? Unfortunately, JS has no such function, so I thought about whether the great JQ encapsulates this function and found the API. JQ indeed encapsulates this function.
jQuery.inArray( value, array ) Searches for the specified value in the array and returns its index (returns -1 if not found).
value to search for value.
array an array, search through it.
Of course, when I was learning, I also wrote such a function:
The code copy is 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, look for the needle in the array, bool is a boolean quantity, if true, return the position of the needle in the array