Chamilyan
Chamilyan

Reputation: 9423

Variable case list in switch statement

I have a somewhat complicated need for a switch statement with a variable case list. It would look like this in pseudo-code..

switch(check){
case 1:
case 2:
..
case etc: do something
break; 
}

in theory the case list would be generated off an array where I don't know the amount of possible case values beforehand.

[1,2,3,4,5 ... ]

is this possible?

Upvotes: 1

Views: 607

Answers (1)

Oleksandr T.
Oleksandr T.

Reputation: 77482

I think impossible programmatically generate cases for switch. Maybe in this case better use if-else with indexOf, like this

var data = [1,2,3,4,5];
var check = 1;

if (data.indexOf(check) >= 0) {
  // to do something
}

Upvotes: 1

Related Questions