Afs*_*ani 692 javascript switch-statement
我在JavaScript中的switch语句中需要多个case,比如:
switch (varName)
{
case "afshin", "saeed", "larry":
alert('Hey');
break;
default:
alert('Default case');
break;
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?如果在JavaScript中没有办法做类似的事情,我想知道一个也遵循DRY概念的替代解决方案.
ken*_*ytm 1370
使用switch语句的fall-through功能.匹配的大小写将一直运行,直到找到break(或switch语句的结尾),因此您可以将其写为:
switch (varName)
{
case "afshin":
case "saeed":
case "larry":
alert('Hey');
break;
default:
alert('Default case');
}
Run Code Online (Sandbox Code Playgroud)
小智 87
这适用于常规JavaScript
function theTest(val) {
var answer = "";
switch( val ) {
case 1: case 2: case 3:
answer = "Low";
break;
case 4: case 5: case 6:
answer = "Mid";
break;
case 7: case 8: case 9:
answer = "High";
break;
default:
answer = "Massive or Tiny?";
}
return answer;
}
theTest(9);
Run Code Online (Sandbox Code Playgroud)
干杯.
elc*_*nrs 42
这是switch完全避免声明的不同方法:
var cases = {
afshin: function() { alert('hey'); },
_default: function() { alert('default'); }
};
cases.larry = cases.saeed = cases.afshin;
cases[ varName ] ? cases[ varName ]() : cases._default();
Run Code Online (Sandbox Code Playgroud)
小智 17
在Js中为switch分配多个case我们必须定义different case without break inbetween如下所示:
<script>
function checkHere(varName){
switch (varName)
{
case "saeed":
case "larry":
case "afshin":
alert('Hey');
break;
case "ss":
alert('ss');
break;
default:
alert('Default case');
break;
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
请参阅示例单击链接
小智 15
我喜欢这样的清晰度和DRY语法。
varName = "larry";
switch (true)
{
case ["afshin", "saeed", "larry"].includes(varName) :
alert('Hey');
break;
default:
alert('Default case');
}
Run Code Online (Sandbox Code Playgroud)
Eri*_*ikE 12
如果您使用的是ES6,则可以执行以下操作:
if (['afshin', 'saeed', 'larry'].includes(varName)) {
alert('Hey');
} else {
alert('Default case');
}
Run Code Online (Sandbox Code Playgroud)
或者对于早期版本的JavaScript,您可以这样做:
if (['afshin', 'saeed', 'larry'].indexOf(varName) !== -1) {
alert('Hey');
} else {
alert('Default case');
}
Run Code Online (Sandbox Code Playgroud)
请注意,这在旧的IE浏览器中不起作用,但您可以相当容易地修补.请参阅问题确定字符串是否在javascript列表中以获取更多信息.
Mik*_*e K 12
我的情况类似于:
switch (text) {
case SOME_CONSTANT || ANOTHER_CONSTANT:
console.log('Case 1 entered');
break;
case THIRD_CONSTANT || FINAL_CONSTANT:
console.log('Case 2 entered');
break;
default:
console.log('Default entered');
}
Run Code Online (Sandbox Code Playgroud)
的default情况下,始终输入。如果您遇到类似的多案例 switch 语句问题,您正在寻找这个:
switch (text) {
case SOME_CONSTANT:
case ANOTHER_CONSTANT:
console.log('Case 1 entered');
break;
case THIRD_CONSTANT:
case FINAL_CONSTANT:
console.log('Case 2 entered');
break;
default:
console.log('Default entered');
}
Run Code Online (Sandbox Code Playgroud)
在 Node.js 中,您似乎可以这样做:
data = "10";
switch(data){
case "1": case "2": case "3": // Put multiple cases on the same
// line to save vertical space.
console.log("small");
break;
case "10": case "11": case "12":
console.log("large");
break;
default:
console.log("strange");
break;
}
Run Code Online (Sandbox Code Playgroud)
在某些情况下,这使得代码更加紧凑。
小智 7
这是一种更易于使用的 switch case 语句。这可以满足您的要求。我们可以使用 switch 语句中的 find 方法来获取所需的输出。
switch(varname){
case["afshin","saeed","larry"].find(name => name === varname):
alert("Hey")
break;
default:
alert('Default case');
break;
}
Run Code Online (Sandbox Code Playgroud)
添加和澄清Stefano的答案,您可以使用表达式以dinamically方式设置switch中条件的值,例如:
var i = 3
switch (i) {
case ((i>=0 && i<=5)?i:-1): console.log('0-5'); break;
case 6: console.log('6');
}
Run Code Online (Sandbox Code Playgroud)
所以在你的问题中,你可以这样做:
var varName = "afshin"
switch (varName) {
case (["afshin", "saeed", "larry"].indexOf(varName)+1 && varName):
console.log("hey");
break;
default:
console.log('Default case');
}
Run Code Online (Sandbox Code Playgroud)
虽然不是那么干......
我像这样使用它:
switch (true){
case /Pressure/.test(sensor):
{
console.log('Its pressure!');
break;
}
case /Temperature/.test(sensor):
{
console.log('Its temperature!');
break;
}
}
Run Code Online (Sandbox Code Playgroud)
你可以使用' in '运算符...
依赖于对象/哈希调用...
所以它和javascript一样快......
// assuming you have defined functions f(), g(a) and h(a,b)
// somewhere in your code
// you can define them inside the object but...
// the code becomes hard to read, I prefer this way
o = { f1:f, f2:g, f3:h };
// if you use "STATIC" code can do:
o['f3']( p1, p2 )
// if your code is someway "DYNAMIC", to prevent false invocations
// m brings the function/method to be invoked (f1, f2, f3)
// and you can rely on arguments[] to solve any parameter problems
if ( m in o ) o[m]()
Run Code Online (Sandbox Code Playgroud)
享受,ZEE
一些有趣的方法。对我来说,解决问题的最佳方法是使用.find.
您可以通过在查找函数中使用合适的名称来指示多种情况。
switch (varName)
{
case ["afshin", "saeed", "larry"].find(firstName => firstName === varName):
alert('Hey');
break;
default:
alert('Default case');
break;
}
Run Code Online (Sandbox Code Playgroud)
其他答案更适合给定的示例,但如果您有多种情况,这是最好的方法。
| 归档时间: |
|
| 查看次数: |
588231 次 |
| 最近记录: |