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
Release: 2020-11-16 15:22:32
Original
3966 people have browsed it

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;));//循环的方式
Copy after login

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);
Copy after login

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!

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
Latest Issues
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template