check if value exists in array

This question already has an answer here:

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

  • var codes = ["US-AL", "US-AZ", ... ];
    
    
    if($.inArray(code, codes) > -1){
        //do something    
    }
    

    更新:结合@ Deestan的建议,这可以重写为..

    function isValidCode(code){
        return ($.inArray(code, codes) > -1);
    }
    

    是的

    array.indexOf(searchElement[, fromIndex])

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

    上一篇: Javascript如果在x中

    下一篇: 检查数组中是否存在值