[英]How can I use ranges in a switch case statement using JavaScript?
如何在使用 JavaScript 的 switch case 語句中使用范圍? 因此,我不想為每一種可能性編寫代碼,而是將它們按范圍分組,例如:
switch(myInterval){
case 0-2:
//doStuffWithFirstRange();
break;
case 3-6:
//doStuffWithSecondRange();
break;
case 6-7:
//doStuffWithThirdRange();
break;
default:
//doStuffWithAllOthers();
}
您至少有四個選擇:
case
如LightStyle所示 ,您可以明確列出每種情況:
switch(myInterval){
case 0:
case 1:
case 2:
doStuffWithFirstRange();
break;
case 3:
case 4:
case 5:
doStuffWithSecondRange();
break;
case 6:
case 7:
doStuffWithThirdRange();
break;
default:
doStuffWithAllOthers();
}
if
/ else if
/ else
如果范圍很大,那將很麻煩,因此您要進行范圍設置。 請注意, if...else if...else if
使用if...else if...else if
,則在較早的匹配時不會進入較晚的匹配,因此您每次都只需指定上限。 為了清楚起見,我將在/*...*/
包含下界,但是通常您會避免使用下界以避免引入維護問題(如果同時包含兩個邊界,則很容易更改一個邊界而忘記更改另一個邊界) :
if (myInterval < 0) {
// I'm guessing this is an error
}
else if (/* myInterval >= 0 && */ myInterval <= 2){
doStuffWithFirstRange();
}
else if (/* myInterval >= 3 && */ myInterval <= 5) {
doStuffWithSecondRange();
}
else if (/* myInterval >= 6 && */ myInterval <= 7) {
doStuffWithThirdRange();
}
else {
doStuffWithAllOthers();
}
case
: JavaScript不常見,因為您可以在case
語句中使用表達式,因此我們可以將上述if...else if...else if
序列編寫為switch
語句:
switch (true){
case myInterval < 0:
// I'm guessing this is an error
break;
case /* myInterval >= 0 && */ myInterval <= 2:
doStuffWithFirstRange();
break;
case /* myInterval >= 3 && */ myInterval <= 5:
doStuffWithSecondRange();
break;
case /* myInterval >= 6 && */ myInterval <= 7:
doStuffWithThirdRange();
break;
default:
doStuffWithAllOthers();
}
我不主張這樣做,但這是 JavaScript中的一個選項,有時它很有用。 根據您在switch
提供的值,按順序檢查case
語句。 (同樣,在許多情況下,下限可能會被省略,因為它們早先會匹配。)即使case
s是按源代碼順序處理的, default
也可以出現在任何地方(不僅在末尾),並且僅在以下情況下處理:或者沒有case
小號匹配或case
匹配,下跌至默認(沒有一個break
,它是罕見的,你想這樣做,但它發生)。
如果所有函數都采用相同的參數(並且可能沒有參數,或者只是相同的參數),則另一種方法是調度映射:
在一些安裝代碼中:
var dispatcher = {
0: doStuffWithFirstRange,
1: doStuffWithFirstRange,
2: doStuffWithFirstRange,
3: doStuffWithSecondRange,
4: doStuffWithSecondRange,
5: doStuffWithSecondRange,
6: doStuffWithThirdRange,
7: doStuffWithThirdRange
};
然后代替開關:
(dispatcher[myInterval] || doStuffWithAllOthers)();
通過查找要在dispatcher
地圖上調用的函數可以正常工作,如果使用奇怪的功能||
,如果沒有針對該特定myInterval
值的條目,則默認為doStuffWithAllOthers
運算符 ,然后調用它。
您可以將其分為兩行,以使其更加清晰:
var f = dispatcher[myInterval] || doStuffWithAllOthers;
f();
我使用了一個對象以獲得最大的靈活性。 您可以通過特定的示例來定義dispatcher
:
var dispatcher = [
/* 0-2 */
doStuffWithFirstRange,
doStuffWithFirstRange,
doStuffWithFirstRange,
/* 3-5 */
doStuffWithSecondRange,
doStuffWithSecondRange,
doStuffWithSecondRange,
/* 6-7 */
doStuffWithThirdRange,
doStuffWithThirdRange
];
...但是,如果這些值不是連續的數字,那么使用一個對象代替它會更清楚。
這也許是您所需要的嗎?
switch(myInterval){
case 0:
case 1:
case 2:
//doStuff();
break;
case 3:
case 4:
case 5:
case 6:
//doStuff();
break;
case 6:
case 7:
//doStuff();
break;
default:
//doStuff();
}
如果您知道范圍將非常大(例如0-100
),則也可以這樣做,這肯定更容易,更清潔和更簡單:
if (myInterval >= 0 && myInterval <= 20) {
//doStuff();
} else if (myInterval > 20 && myInterval <= 60) {
//doStuff();
} else if (myInterval > 60 && myInterval <= 70) {
//doStuff();
} else /* it is greater than 70 */ {
//doStuff();
}
此示例中的范圍很小,但是根據JavaScript MDN Docs ,這是處理更大范圍的方法:
// The value we'll be evaluating:
let code = 100;
// Matches for any case where the expression === `true`:
switch (true) {
case code <= 64:
return "Your number is 64 or less!";
break;
case code >= 65 && code <= 90:
return "Your number is in the range of 65-90!";
break;
case code >= 97 && code <= 122:
return "Your number is in the range of 97-122!";
break;
case code >= 123:
return "Your number is 123 or greater!";
break;
default:
break;
}
我知道TJ Crowder已經通過用case
和Expressions展示了這種風格,但是我只是想展示另一個如何利用這種方法的示例。 我只是這樣做,並以為其他示例可能會對某人有所幫助,因為在閱讀其他回復后我仍然有些困惑。
如果范圍相同並且從0開始,則可以進行一些數學運算。
doStuffWithRange(Math.floor(myInterval/range));
例如,如果您想像地圖一樣將RED,GREEN和BLUE放在地圖上,例如:
你可以寫:
function colorInterval(n, max) {
var colors = ["RED", "GREEN", "BLUE"];
var range = max/colors.length
return colors[Math.floor(n/range)];
}
//You get 3 of RED, 3 of GREEN, 2 of BLUE
for (var i=0; i<8; i++) {
console.log(colorInterval(i, 8));
}
請注意,示例中的最后一個范圍是2,而不是3,只要先前的范圍相同,它仍然有效。
為了給已經發布的出色答案增加多樣性,尤其是當間隔以0開頭時,這是findIndex
( Yeah ES6 )的解決方案:
const range = [0, 2, 6, 7];
const randeIndex = range.findIndex(threshold => myInterval <= threshold);
switch (rangeIndex) {
case 1:
//doStuffWithFirstRange();
break;
case 2:
//doStuffWithSecondRange();
break;
case 3:
//doStuffWithThirdRange();
break;
default:
//doStuffWithAllOthers();
}
由於range
數組是有序的,因此findIndex
將與第一個匹配。 根據范圍的命名方式 ,從0或1開始,您可能需要刪除range
中的第一個0。
用例語句帶有已定義的字符串或值,或在范圍更大的情況下使用“ if else if”
int levelNumber =您的FROM_VALUE
NSString* strMessage;
switch (levelNumber) {
case 1...10:
{
// Do something...
break;
}
case 11...20:
{
// Do something...
break;
}
case 21...30:
{
// Do something...
break;
}
case 31...40:
{
// Do something...
break;
}
default:
break;
}
請參閱: https : //www.codingexplorer.com/loops-switch-statements-ranges-swift/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.