javascript, jQuery - 检查数组中是否存在值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11779394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:03:29  来源:igfitidea点击:

javascript, jQuery - check if value exists in array

javascriptjqueryarraysjvectormap

提问by Aaron

Is there a better way to check multiple items match a variable in if statement

有没有更好的方法来检查多个项目与 if 语句中的变量匹配

I have 3 if statements and i need to see if a item matches an array/variable named code. There are a lot of items to compare against the code array/variable so i am having to duplicate each one with the | between them. Is there a more efficient way or could i make an array to use once to check is the code equals any of the items in the array?

我有 3 个 if 语句,我需要查看一个项目是否与命名代码的数组/变量匹配。有很多项目要与代码数组/变量进行比较,所以我不得不用 | 复制每个项目。它们之间。有没有更有效的方法,或者我可以制作一个数组来使用一次来检查代码是否等于数组中的任何项目?

This is what i have

这就是我所拥有的

onLabelShow: function(event, label, code){
        if (code == "US-AL" | code == "US-AZ" | code == "US-CA" | code == "US-CT" | code == "US-FL" | code == "US-HI" | code == "US-ID" | code == "US-IL" | code == "US-IA" | code == "US-LA" | code == "US-ME" | code == "US-MD" | code == "US-MA" | code == "US-MO" | code == "US-NE" | code == "US-NV" | code == "US-NJ" | code == "US-NM" | code == "US-NY" | code == "US-OH" | code == "US-OK" | code == "US-OR" | code == "US-PA" | code == "US-TN" | code == "US-UT" | code == "US-VA" | code == "US-CA" | code == "US-WA" | code == "US-NC" ) {
            //do nothing
        }
        else if (code) { //if a state is not specified in var stateRedirects then prevent default
            event.preventDefault();
        }                   
    },      
    onRegionOver: function(event, code){
        if (code == "US-AL" | code == "US-AZ" | code == "US-CA" | code == "US-CT" | code == "US-FL" | code == "US-HI" | code == "US-ID" | code == "US-IL" | code == "US-IA" | code == "US-LA" | code == "US-ME" | code == "US-MD" | code == "US-MA" | code == "US-MO" | code == "US-NE" | code == "US-NV" | code == "US-NJ" | code == "US-NM" | code == "US-NY" | code == "US-OH" | code == "US-OK" | code == "US-OR" | code == "US-PA" | code == "US-TN" | code == "US-UT" | code == "US-VA" | code == "US-CA" | code == "US-WA" | code == "US-NC" ) {
            //do nothing
        }
        else if (code) { //if a state is not specified in var stateRedirects then prevent default
            event.preventDefault();
        }                   
    },
    onRegionClick: function (event, code) {
        if (code == "US-AL" | code == "US-AZ" | code == "US-CA" | code == "US-CT" | code == "US-FL" | code == "US-HI" | code == "US-ID" | code == "US-IL" | code == "US-IA" | code == "US-LA" | code == "US-ME" | code == "US-MD" | code == "US-MA" | code == "US-MO" | code == "US-NE" | code == "US-NV" | code == "US-NJ" | code == "US-NM" | code == "US-NY" | code == "US-OH" | code == "US-OK" | code == "US-OR" | code == "US-PA" | code == "US-TN" | code == "US-UT" | code == "US-VA" | code == "US-CA" | code == "US-WA" | code == "US-NC" ) {
            window.location = '/' + code;
        }
        else if (code) { //if a state is not specified in var stateRedirects then prevent default
            //event.preventDefault();
        }   

    }   

Any help, examples or code would be greatly appreciated! I have tried doing for each but think i do not know enough to get it to work with an array.

任何帮助、示例或代码将不胜感激!我试过为每个做,但认为我不知道足以让它与数组一起工作。

回答by Robin Maben

var codes = ["US-AL", "US-AZ", ... ];


if($.inArray(code, codes) > -1){
    //do something    
}

UPDATE:Incorporating @Deestan's suggestion, this could be re-written as..

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

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

回答by Radu

Yep

是的

array.indexOf(searchElement[, fromIndex])

array.indexOf(searchElement[, fromIndex])