I was wondering if it is possible to rewrite multiple if
statements into a switch
.
The problem is that a switch
runs:
all code after a case passes a check. Which is why the case statement runs all code after the first case.
let arr = [1, 3]; if( arr.includes(1) === true ) { console.log('if 1'); } if( arr.includes(2) === true) { console.log('if 2'); } if( arr.includes(3) === true) { console.log('if 3'); } switch( true ){ case arr.includes(1): console.log('switch 1'); case arr.includes(2): console.log('switch 2'); case arr.includes(3): console.log('switch 3'); }
let arr = [1, 3]; if( arr.includes(1) === true ) { console.log('if 1'); } if( arr.includes(2) === true) { console.log('if 2'); } if( arr.includes(3) === true) { console.log('if 3'); } switch( true ){ case arr.includes(1): console.log('switch 1'); break; case arr.includes(2): console.log('switch 2'); break; case arr.includes(3): console.log('switch 3'); break; }
So the question is: How can I rewrite multiple if
statements into a single switch
statement?
If I can't: Is there another more elegant syntax than the multiple if
statements, that makes it apparent that I am making similar comparisons?
How can I rewrite multiple if statements into a single switch statement?
You can't, reasonably, if you want multiple cases to match. switch
can replace if
/ else
, but not a series of independent if
s where more than one can match.
Is there another more elegant syntax than the multiple if statements, that makes it apparent that I am making similar comparisons?
The answer here will tend to be specific to the code you're writing. A couple of options for you:
Whenever you have code where you're doing the same thing over and over again, parameterize it and put it in a function, then call the function repeatedly with the parameters.
function doTheThing(value) {
if (arr.includes(value)) {
console.log('xyz ' + value);
}
}
For instance, in your example:
function doTheThing(value) { if (arr.includes(value)) { console.log('xyz ' + value); } } let arr = [1, 3]; doTheThing(1); doTheThing(2); doTheThing(3);
or
let arr = [1, 3]; [1, 2, 3].forEach(value => { if (arr.includes(value)) { console.log("xyz " + value); } });
or combining those:
function doTheThing(value) { if (arr.includes(value)) { console.log('xyz ' + value); } } let arr = [1, 3]; [1, 2, 3].forEach(doTheThing);
If you're doing different things, one common practice is to have a lookup table of value-to-action, eg:
const actionsByValue = { 1() { console.log("This is the thing for #1"); }, 2() { console.log("This is something else for #2"); }, 3() { console.log("Different logic again for #3"); } }; const nop = () => { }; let arr = [1, 3]; arr.forEach(value => { (actionsByValue[value] || nop)(value); });
That 1() { }
notation may look odd because you don't see method notation with properties with numeric names very often, but it's perfectly valid. In old environments that don't support method notation:
const actionsByValue = {
1: function() {
console.log("This is the thing for #1");
},
2: function() {
console.log("This is something else for #2");
},
3: function() {
console.log("Different logic again for #3");
}
};
Side note: You never need === true
with Array#includes
. It always returns a boolean.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.