Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 905
Aaron Avatar asked Aug 02 '12 14:08

Aaron


People also ask

How do you check if a value is repeated in an array JavaScript?

To check if an array contains duplicates: Use the Array. some() method to iterate over the array. Check if the index of the first occurrence of the current value is NOT equal to the index of its last occurrence. If the condition is met, then the array contains duplicates.

How do I check if an array contains duplicates?

function checkIfArrayIsUnique(myArray) { for (var i = 0; i < myArray. length; i++) { for (var j = 0; j < myArray. length; j++) { if (i != j) { if (myArray[i] == myArray[j]) { return true; // means there are duplicate values } } } } return false; // means there are no duplicate values. }

How do you check if a value exists in an array in jQuery?

inArray() This jQuery array method search the the item within the array. If element exists in the jQuery array it returns the index position of the value and if the value doesn't exist then it will return -1 .

How can check duplicate value in textbox using jQuery?

Using each() check value of inputs and if any value is duplicate add class duplicate to it.


Video Answer


1 Answers

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);
}
like image 79
Robin Maben Avatar answered Sep 22 '22 13:09

Robin Maben