javascript, jQuery - check if value exists in array [duplicate]
Asked Answered
M

2

15

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.

Minuteman answered 2/8, 2012 at 14:18 Comment(1)
See this for more discussion: #237604Aleris
S
52
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);
}
Seawright answered 2/8, 2012 at 14:20 Comment(6)
I'd also recommend wrapping this in a function isValidCode.Principled
Probably the better solution if you're already using jQuery. Internal implementation of $.inArray: jsfiddle.net/HB3jrAleris
+1 for also showing how to declare the array in the first place, which could easily have been the next question given that the questioner admitted not knowing enough to solve their problem with arrays.Rosemarierosemary
Another option if you're using underscore: underscorejs.org/#indexOfAleris
wow! this looks great! let me see if i can figure out how to replace what i have. So should i use the isValidCode in the if statement?Minuteman
@Aaron: Yes. Also, since all your handlers do the same thing, you should make that a function as a whole and use it in all places like - onRegionClick : checkCode, onRegionOver : checkCode ... and so on for the others.Seawright
A
15

Yep

array.indexOf(searchElement[, fromIndex])

Aleris answered 2/8, 2012 at 14:20 Comment(1)
That's right too. :) He tagged it jQuery so I thought why not use the jQuery equivalent.Seawright

© 2022 - 2024 — McMap. All rights reserved.