Home > Web Front-end > JS Tutorial > How to check if there is a specified value in an array with jquery?

How to check if there is a specified value in an array with jquery?

青灯夜游
Release: 2020-11-19 16:30:32
Original
3149 people have browsed it

In jquery, you can use the "$.inArray()" method to query whether there is a value in the array. This method can search for the specified value in the array and return the index value of the specified value in the array. If the specified value does not exist in the array, "-1" is returned; syntax "$.inArray(value,array)".

How to check if there is a specified value in an array with jquery?

Related recommendations: "jQuery Tutorial"

jquery determines whether the specified value exists in the array The value method $.inArray()

##$.inArray() method is used to search for the specified value in the array and returns the index value of the specified value in the array. If the array If the specified value does not exist, -1 is returned.

Syntax

$.inArray( value, array [, fromIndex ] )
Copy after login

Parameters:

  • value Any type The value used for the search.

  • array Array type specifies the array to be searched.

  • fromIndex Optional. The Number type specifies that the search starts from the specified index position of the array. The default is 0

Example 1:Return the index value

var arrayStr = ['a','b','c'];
alert($.inArray('a', arrayStr));
Copy after login

The return index value is

0

Example 2: Judge whether the specified value exists

var arrayStr = ['a','b','c'];
if($.inArray(&#39;a&#39;,arrayStr) < 0){
    alert(&#39;不存在&#39;);
}else{
    alert(&#39;存在&#39;);
}
Copy after login

PS: The index value in js starts from subscript 0, if it is -1 It means that the element does not exist in the array

For more programming-related knowledge, please visit:

Programming Video! !

The above is the detailed content of How to check if there is a specified value in an array with jquery?. For more information, please follow other related articles on the PHP Chinese website!

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