Home > Web Front-end > JS Tutorial > body text

How to determine if a value is in an array in javascript

青灯夜游
Release: 2021-12-07 14:46:37
Original
9118 people have browsed it

Method: 1. Use the for statement to traverse the array and compare the array elements with the specified value. If they are equal, they exist; 2. Use the indexOf method to return the subscript value if the element exists in the array. If it does not exist, it will return the subscript value. Return "-1"; 3. Use the inArray() method to return the subscript value if the element exists, and return "-1" if it does not exist.

How to determine if a value is in an array in javascript

The operating environment of this tutorial: windows7 system, javascript1.8.5&&jquery1.10.2 version, Dell G3 computer.

There is an array in js. How to determine whether an element exists in this array? First, it is determined through a loop. The code is as follows:

var arr = ['a','s','d','f'];
console.info(isInArray(arr,'a'));//循环的方式

/**
 * 使用循环的方式判断一个元素是否存在于一个数组中
 * @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;
}
Copy after login

This method is more general. method, but you need to write the function yourself. Let’s take a look at the second method:

var arr = [&#39;a&#39;,&#39;s&#39;,&#39;d&#39;,&#39;f&#39;];
console.info(arr.indexOf(&#39;a&#39;));//在IE某些版本中不支持,可以自行扩展
Copy after login

This method directly uses the indexOf method of the array to determine. If the element exists in the array, then the returned element is in the array. If the subscript value does not exist, then -1 is returned. Note that indexOf is case-sensitive, and the letter O must be capitalized, otherwise an error will be reported. In addition, this method is not available in some versions of IE. It will not work, so you need to make a judgment before using it. The modified code is as follows:

/**
 * 使用indexOf判断元素是否存在于数组中
 * @param {Object} arr 数组
 * @param {Object} value 元素值
 */
function isInArray3(arr,value){
    if(arr.indexOf&&typeof(arr.indexOf)==&#39;function&#39;){
        var index = arr.indexOf(value);
        if(index >= 0){
            return true;
        }
    }
    return false;
}
Copy after login

The third way is to use jquery's inArray method, which returns the element in the array If the subscript does not exist in the array, then -1 is returned. The code is as follows:

/**
 * 使用jquery的inArray方法判断元素是否存在于数组中
 * @param {Object} arr 数组
 * @param {Object} value 元素值
 */
function isInArray2(arr,value){
    var index = $.inArray(value,arr);
    if(index >= 0){
        return true;
    }
    return false;
}
Copy after login

This method can be used to delete elements with unknown subscript values ​​in an array. The code is as follows:

var arr = [&#39;a&#39;,&#39;s&#39;,&#39;d&#39;,&#39;f&#39;];
console.info("删除元素之前的数组:"+arr);
arr.splice($.inArray(&#39;a&#39;,arr),1);
console.info("删除元素之后的数组:"+arr);
Copy after login

The execution result is:

[Web浏览器] "删除元素之前的数组:a,s,d,f"    /test/index.html (12)
[Web浏览器] "删除元素之后的数组:s,d,f"  /test/index.html (14)
Copy after login

[Related recommendations: javascript learning tutorial]

The above is the detailed content of How to determine if a value is in an array in javascript. 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