【发布时间】:2018-03-03 10:37:05
【问题描述】:
我很难运行在 switch case 中检查两个条件的代码,因为在 javascript 中此代码打印“未知”
['police',false]!=['police',false]
有没有办法使用switch-case 而不是嵌套的ifs 来实现这段代码?
var option='police';
var urgent=false;
switch([option,urgent])
{
case ['police',true]:
console.log('police,true');
call_police_urgent();
break;
case ['police',false]:
console.log('police,false');
call_police();
break;
case ['hospital',true]:
console.log('hospital,true');
call_hospital_urgent();
break;
case ['hospital',false]:
console.log('hospital,false');
call_hospital();
break;
case ['firestation',true]:
console.log('firestation,true');
call_firestation_urgent();
break;
case ['firestation',false]:
console.log('firestation,false');
call_firestation();
break;
default:
console.log('unknown');
}
【问题讨论】:
-
错字:
swtich=>switch -
@georg 谢谢。但主要问题仍然存在
-
@ar2015:是的,见下文
-
@ecg8, 提供的解决方案并不比
if好。
标签: javascript node.js