JS script to find a string in a set

mouslander

23-04-2015

I have a large list of items in a drop down ~30. Based on the selection I need to generate some options. I can do this several ways but figure there has to be a better way.

My first thought is:

var a = dropdownList1.rawValue

if (a == "Option1" || a == "Option2" || etc){

then do something

}

else

if(a == "Category1" || a== "Category2" etc){

then do something else

}

I can also use a case statement but cannot find a way to say:

case "Option1" || "Option 2"

This would leave me with

case "Option1

this.rawValue = ??

break

case "Option 2

There has to be a better way. I am thinking I could use an array but not sure what the syntax would be

Accepted Solutions (0)

Answers (2)

Answers (2)

mouslander

23-04-2015

Thank you for your reply. I will have to try it. I thought I tried that but could not get it to work. Probably a syntax error on my part. I will have to try it again. I broke the items down and wrote it as an if statement. It is a bit verbose but it works.

jasotastic81

23-04-2015

It's not exactly using "or" in a switch statement, but it is logically equivalent. The dropdown has values a, b, c, and d.

754296_pastedImage_0.png

How it works: Since there is no "break" command inside of case "a", the switch statement continues through case "b", but doesn't do another check. It breaks from the switch when it sees the break command. Similarly, selecting "c" will get the same result as "d". (Since I control the options in the drop down list, the break is not strictly necessary, but I put it in anyway out of habit.)