Home >Web Front-end >JS Tutorial >How to determine whether a value is in an array in jquery

How to determine whether a value is in an array in jquery

青灯夜游
青灯夜游Original
2021-11-12 15:43:233195browse

In jquery, you can use the inArray() method to determine whether the value is in the array. The syntax is "$.inArray(value,array)"; if the specified value is in the array, it will return the value in the array. The subscript (index) of , if not, returns "-1".

How to determine whether a value is in an array in jquery

The operating environment of this tutorial: windows7 system, jquery1.10.0 version, Dell G3 computer.

jquery determines whether the value is in the array

In jquery, you can use the inArray() method to determine whether the value is in the array.

$.inArray() function is used to find the specified value in the array and return its index value (if not found, it returns -1).

Using this feature, we can use the $.inArray() function to find the specified value in the array and see if -1 is returned to determine whether the value is in the array.

Implementation code:

<script src="js/jquery-1.10.0.min.js"></script>
<script>
	var array=[1,2,3,4,5,6];
	var value=2;
	var vlaue = $.inArray(value, array);
	if(value != -1) {
	  console.log("指定值在数组中");
	}
	else{
		console.log("指定值不在数组中");
	}
</script>

How to determine whether a value is in an array in jquery

Related video tutorial recommendations: jQuery tutorial (video)

The above is the detailed content of How to determine whether a value is in an array in jquery. For more information, please follow other related articles on the PHP Chinese website!

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