I have an array that will be populated with an unknown number of elements.
myArray = ["a", "b", "c",...]
I want to create a switch statement so that it contains all the cases contained in myArray.
The outcome I would like is something like:
switch (true) {
case myVariable === 'a':
// DO something
break
case myVariable === 'b':
// DO something
break
... KEEP REPETING FOR EACH ELEMENT IN myArray
}
Clearly, I cannot hardcode all the cases because I do not know how many there are and how they are called.
My idea was to create a loop inside the switch as follow:
switch (true) {
myArray.map(myElement => {
case myVariable === myElement:
// DO something
break
})
}
I do not think this is possible because switch expects a case not a .map
I check this answer Dynamically adding cases to a switch but it does not address the loop part
forloop would suffice.eval(). However I would not recommend to use that since it has downsides because of security and performance. As others mentioned a simple loop would do the trick too.