我有一個開關語句,並且對於每種情況都有一個這種情況的負載情況。檢查開關櫃內的大量情況
這是我到目前爲止。
exports.message = function message(message) {
switch (message.toLowerCase()) {
case "un":
case "one":
case "uno":
case "um":
case "unus":
case "ano":
case "un":
//100 + other cases...
return "Returned msg here"
break;
default: return "Sorry, I didn't quite understand that."
}
}
在互聯網上尋找答案後,我找到了所有我能找到的答案,但這不適合我。
由於我的一些案件有多達200個不同的「案件」,我正在尋找另一種方式。因爲這樣做不僅醜陋,長(200行只是一個案例),但如果我想改變任何東西,也很難操縱。
我會像最好是這樣的:
exports.message = function message(message) {
switch (message.toLowerCase()) {
case ["un", "one", "uno", "um", "unus", "ano", "un", /* 100+ other cases...*/]
return "Returned msg here"
break;
default: return "Sorry, I didn't quite understand that."
}
}
什麼是做到這一點的最好方法是什麼?
謝謝!
開關塊不適用於大量可能的值。只需使用容器對象(如Array)並測試值是否存在。 – faintsignal