Find out if a variable is in an array?

This question already has an answer here:

  • How do I check if an array includes an object in JavaScript? 40 answers

  • jQuery has a utility function to find whether an element exist in array or not

    $.inArray(value, array)
    

    It returns index of the value in array and -1 if value is not present in array. so your code can be like this

    if( $.inArray(code, countryList) != -1){
         alert('value is Array!');
    } else {
        alert('Not an array');
    }
    

    You need to use Array.indexOf :

    if (countryList.indexOf(code) >= 0) {
       // do stuff here
    }
    

    Please not that it is not supported in and before IE8 (and possibly other legacy browsers). Find out more about it here.


    你似乎在寻找Array.indexOf函数。

    链接地址: http://www.djcxy.com/p/13046.html

    上一篇: 我怎样才能比较一个字符串与几个值?

    下一篇: 找出一个变量是否在数组中?