[英]Simplify This Conditional Statement
I have this crazy conditional that I just know there is a better way to write. 我有这种疯狂的条件,我只知道有更好的写作方法。 Here is the pseudo code:
这是伪代码:
if( A && B ){
console.log( 'do not show' );
}
if( A && C ){
console.log( 'show' );
}
if( A && D ){
console.log( 'do not show' );
}
if( A && E ) {
console.log( 'do not show' );
}
I'm just really blanking out. 我真的很茫然。
How about this: 这个怎么样:
if (A) {
if (B || D || E) {
console.log( 'do not show' );
}
if ( C ) {
console.log( 'show' );
}
}
If you really wanted to specify separate tasks for each condition, then you could use this, but it's overly complicated if your task is just the above code. 如果您确实想为每个条件指定单独的任务,则可以使用它,但是如果您的任务只是上面的代码,那就太复杂了。
var A = C = true;
var B = D = E = false;
function doWhen( cond, fn ) {
if ( cond === true ) {
return ( typeof fn === 'function' ? fn() : fn );
}
return undefined;
}
doWhen(A, function() {
[ [B, function() { console.log('b message') }],
[C, function() { console.log('c message') }],
[D, function() { console.log('d message') }],
[E, function() { console.log('e message') }] ]
.forEach(function(cond) {
doWhen.apply(null, cond);
});
});
Wouldn't this do it: 这样不行吗?
if (A) {
if(B || D || E){
console.log( 'do not show' );
}
if( C ){
console.log( 'show' );
}
}
update 更新
Or just for fun (if you want every state) 或只是为了好玩(如果您想要每个州)
switch((a << 0) | (b << 1) | (c << 2) | (d << 3) | (e << 4)) {
case 3: console.log("A && B"); break;
case 5: console.log("A && C"); break;
case 9: console.log("A && D"); break;
case 17: console.log("A && E"); break;
}
A JSFiddle to play with: http://jsfiddle.net/LxvBZ/ 可以使用的JSFiddle: http : //jsfiddle.net/LxvBZ/
If you really needed to evaluate B
, C
, D
, and E
separately, this is pretty straight forward: 如果确实需要分别评估
B
, C
, D
和E
,那么这很简单:
if( A ){
if( B ){
console.log( 'do not show' );
}
if( C ){
console.log( 'show' );
}
if( D ){
console.log( 'do not show' );
}
if( E ) {
console.log( 'do not show' );
}
}
Now if all those if
's (after the first) were supposed to be else if
's, then this can be simplified to: 现在,如果所有的
if
(在第一个之后)都应该是else if
,则可以简化为:
if( A ){
if( B || D || E ){
console.log( 'do not show' );
}else if( C ){
console.log( 'show' );
}
}
I would do: 我会做:
if ( A ) {
if( B || D || E ){
console.log( 'do not show' );
}
if( C ){
console.log( 'show' );
}
}
if( A ){
var printOnLog = '';
if( B || C || E ){
printOnLog = 'do not show';
}
else if( C ){
printOnLog = 'show';
}
else {
//default action
}
console.log(printOnLog);
}
Well, I think that it's impossible to have multiple conditions, if this is true use "else if" to avoid usless if. 好吧,我认为不可能有多个条件,如果确实如此,请使用“ else if”以避免无用的if。
If it's possible (A && B) && (A && C), just use normal "if" 如果有可能(A && B)&&(A && C),只需使用常规的“ if”
You could do this: 您可以这样做:
if (A) {
if (C) {
console.log("show");
}
else if (B || D || E) {
console.log("do not show");
}
}
or this if you prefer: 或者,如果您愿意:
if (A && C) {
console.log("show");
}
else if (A && (B || D || E)) {
console.log("do not show");
}
Well, a ternary operator is kind of the same thing. 嗯,三元运算符是一回事。 You could use it to shrink the code.
您可以使用它来缩小代码。
To add more conditions to the ternary operator, use ' ? 要为三元运算符添加更多条件,请使用'? ' to specify if conditions and ' : ' for specifying else conditions.
'指定条件,':'指定其他条件。 It has to end with a final else condition however, like shown below.
但是,它必须以最终的else条件结束,如下所示。
var output = ((A && B) || (A && D) || (A && E)) ? "do not show" : (A && C) ? "show" : "";
console.log(output);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.