Home >Web Front-end >JS Tutorial >How to determine whether an array contains specified elements in jquery

How to determine whether an array contains specified elements in jquery

coldplay.xixi
coldplay.xixiOriginal
2020-11-16 15:22:323934browse

Jquery method to determine whether the array contains the specified element: 1. Use the js method to determine, the code is [console.log(isInArray(arr,'java'))]; 2. Use the jQuery method to determine, The code is [var index = $.inArray()].

How to determine whether an array contains specified elements in jquery

Recommended: "jquery video tutorial"

Jquery method to determine whether an array contains the specified element:

1. js method to determine

/**
* 使用循环的方式判断一个元素是否存在于一个数组中
* @param {Object} arr 数组
* @param {Object} value 元素值
*/
function isInArray(arr,value){
for(var i = 0; i < arr.length; i++){
if(value === arr[i]){
return true;
}
}
return false;
}
console.log(isInArray(arr,&#39;java&#39;));//循环的方式

2. jQuery method to determine

/**
* 使用jquery的inArray方法判断元素是否存在于数组中
* @param {Object} arr 数组
* @param {Object} value 元素值
*/
var index = $.inArray(value,arr);//返回下标值,index>=0时表示该数组包含指定元素,否则表示不存在;
console.log($.inArray(&#39;java&#39;,arr)>=0);

If the project Introducing the jq package, the author recommends the second method. If the first method is not available, the problem can be solved;

Related free learning recommendations: JavaScript (video)

The above is the detailed content of How to determine whether an array contains specified elements 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