I have an array like var mya = ["someval1", "someotherval1", "someval2", "someval3"];
, and I have a function that receives an object with a property set to one of those names.
It makes sense to iterate over the array and for each one check if I can find it in the array, like in a for
statement. But it seems like a more efficient method would use a switch
statement in this case, because the array is static and the task when I find the property is dependent on which property is found.
How do I do this with a switch array? Something like this pseudo code:
switch(mya.forEach) {
case "someval1":
alert("someval1");
break;
...
default:
alert("default");
break;
}
but this only calls once.
Both answers given are the code I already have — I guess there is no cleaner foreach
formulation of switch
.