Home >Web Front-end >JS Tutorial >How to determine whether an array contains specified elements in jquery
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()].
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,'java'));//循环的方式
2. jQuery method to determine
/** * 使用jquery的inArray方法判断元素是否存在于数组中 * @param {Object} arr 数组 * @param {Object} value 元素值 */ var index = $.inArray(value,arr);//返回下标值,index>=0时表示该数组包含指定元素,否则表示不存在; console.log($.inArray('java',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!