Home  >  Article  >  Web Front-end  >  JavaScript version of in_array function (determining whether a specific value exists in an array)_javascript skills

JavaScript version of in_array function (determining whether a specific value exists in an array)_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:49:041314browse

We often use this same logic to determine whether a string or number is in an array. Many programming languages ​​have such specialized functions, such as PHP's in_array(). So does JS have such a function? Unfortunately, JS does not have such a function, so I thought about whether the great JQ encapsulated this function and found the API. JQ did encapsulate this function
jQuery.inArray( value, array ) Search Returns the specified value in an array and returns its index (or -1 if not found).
value The value to search for.
array is an array through which to search.

Of course, in the process of learning, I also wrote such a function myself:

Copy the code The code is as follows:

function inArray1(needle,array,bool){
if(typeof needle=="string"||typeof needle=="number"){
for(var i in array){
                                                                                                                          return true;                              false;
}
}



Three parameters, search for needle in array, bool is a Boolean quantity, if true, return the position of needle in array
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn