Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
33
rated 0 times [  37] [ 4]  / answers: 1 / hits: 66660  / 12 Years ago, thu, august 2, 2012, 12:00:00

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.


More From » jquery

 Answers
72
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);
}

[#83900] Wednesday, August 1, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kalli

Total Points: 589
Total Questions: 105
Total Answers: 97

Location: Rwanda
Member since Thu, Feb 10, 2022
2 Years ago
;