javascript, jQuery - check if value exists in array

Aaron picture Aaron · Aug 2, 2012 · Viewed 64.9k times · Source

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

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?

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.

Answer

Robin Maben picture Robin Maben · Aug 2, 2012
var codes = ["US-AL", "US-AZ", ... ];


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

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

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