Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 16:49:04
Original
1318 people have browsed it

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
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template