JS script to find a string in a set

Avatar

Avatar
Validate 1
Level 4
mouslander
Level 4

Likes

23 likes

Total Posts

298 posts

Correct reply

27 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile

Avatar
Validate 1
Level 4
mouslander
Level 4

Likes

23 likes

Total Posts

298 posts

Correct reply

27 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile
mouslander
Level 4

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)

Avatar

Avatar
Validate 1
Level 4
mouslander
Level 4

Likes

23 likes

Total Posts

298 posts

Correct reply

27 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile

Avatar
Validate 1
Level 4
mouslander
Level 4

Likes

23 likes

Total Posts

298 posts

Correct reply

27 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile
mouslander
Level 4

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.

Avatar

Avatar
Validate 1
Level 4
jasotastic81
Level 4

Likes

35 likes

Total Posts

329 posts

Correct reply

43 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 25
Boost 10
View profile

Avatar
Validate 1
Level 4
jasotastic81
Level 4

Likes

35 likes

Total Posts

329 posts

Correct reply

43 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 25
Boost 10
View profile
jasotastic81
Level 4

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.)