如何优化大量if-else if-else表达式

时间:2017-05-31 07:00:57

标签: javascript if-statement listjs

这是代码的一些示例行。

if(loc > 0 || cat > 0 || price > 0 || jsBed <= bedroom || jsBuilt >= built) {    
 /// Condition to checn all true
    return true;
} else if(loc < 0 || cat > 0 || price > 0 || jsBed <= bedroom || jsBuilt >= built) { 
   /////// 1 false other are true 

} else if(loc > 0 || cat < 0 || price > 0 || jsBed <= bedroom || jsBuilt >= built) { 

}

如何处理这些情况。 如果我有5个声明。然后它必须是一个接一个的12+条件..如果我检查所有5个组合,它会更多的代码行,我们有更好的选择来检查所有条件。

5 个答案:

答案 0 :(得分:12)

如果您在javascript中将布尔表达式视为整数,它将评估为0(对于false)或1(对于true)。因此,您可以对条件求和,然后使用switch-case结构来检查有多少是真的:

var numTrue = 
   (loc > 0) + (cat > 0) + (price > 0) + (jsBed <= bedroom) + (jsBuilt >= built);

switch(numTrue) {
    case 0:
        // do something if no condition is met
        break;
    case 1:
        // do something else if one condition is met
        break;
    // etc...
}

答案 1 :(得分:4)

你的条件永远不会得到满足:

if(loc > 0 || cat > 0 || price > 0 || jsBed <= bedroom || jsBuilt >= built){    
    /// Condition to checn all true
    return true;
} else if(loc < 0 || cat > 0 || price > 0 || jsBed <= bedroom || jsBuilt >= built) { 
/////// 1 false other are true 

} else if(loc > 0 || cat < 0 || price > 0 || jsBed <= bedroom || jsBuilt >= built) { 

}

基本上:

  • 在第二个if上,条件cat > 0 || price > 0 || jsBed <= bedroom || jsBuilt >= built无效,因为第一个条件已经满足。由于您使用的是else if,因此他们已经输入了第一个if。所以唯一重要的是loc < 0
  • 相同的上一次elseifcat < 0相关。

所以它可以改写为

if(loc > 0 || cat > 0 || price > 0 || jsBed <= bedroom || jsBuilt >= built){    
    /// Condition to checn all true
    return true;
} else if(loc < 0) { 
/////// 1 false other are true 

} else if(cat < 0) { 

}

这个答案假设您提供的代码是您尝试简化的代码而不是通用样本。

注意:我认为你可能没有写下你想做的事,忘记了一些AND而不是OR。

答案 2 :(得分:2)

5个条件是2 ** 5,即32个组合。

如果要检查各种组合,而不重复测试,可以对各个结果进行位移,并将它们组合成一个switch语句。 直接使用数字是简洁但不太可读

var loc=1,cat=0,price=0,jsBed=1,bedroom=0,jsbuilt=0,built=1;

let results=[loc > 0,cat > 0,price > 0,jsBed <= bedroom,jsbuilt >= built];
let bits=results.reduce( (accum,current,index)=>accum+(current<<index), 0);
switch(bits){
case 0: // none
 break;
case 3: // first two
 break;
case 4: // third one
 break;
}

将其修改为使用常量会使switch语句更具可读性

var loc=0,cat=1,price=0,jsBed=1,bedroom=0,jsbuilt=0,built=1;

const locBit=1<<0;
const catBit=1<<1;
const priceBit=1<<2;
const bedBit=1<<3;
const builtBit=1<<4;
let bits=( loc > 0 )*locBit |
         ( cat > 0 )*catBit |
         ( price > 0 )*priceBit |
         ( jsBed <= bedroom )*bedBit |
         ( jsbuilt >= built )*builtBit;
switch(bits){
  case 0:
      console.log("!loc,!cat,!price,!bed,!built");
      break;
  case catBit|locBit:
      console.log("loc,cat,!price,!bed>!built");
      break;
  default:
      console.log(bits);
}

你可以使用常数来帮助

答案 3 :(得分:1)

edit1:修改为javascript,而不是java。糟糕...

我不确定您是否要查看所有组合,但您可以通过为每个可能的输出引入数值来对它们进行分组。

具体而言,每个变量有5个变量和2个选项?我已经设置了一个二进制表示数字的表。如果有&gt;每个(或某些)变量必须使用2个选项(基数为10)。您可以使用二进制值,如

const locVal  = (loc > 0 ? 0x1 : 0x0) << 0;
const catVal  = (cat < 0 ? 0x1 : 0x0) << 1;
const priceVal= (price < 0 ? 0x1 : 0x0) << 2;
ect

所以你可以用方法对它们进行分组:

function foo(trueCond, level) {
    return (trueCond ? 0b1 : 0b0) << level;
}

使

const locVal  = foo(loc > 0, 0);
const catVal  = foo(cat > 0, 1);
const priceVal= foo(price > 0, 2)

(我省略了其他变量......)然后加上二进制值

const total = locVal + catVal + priceVal

然后你现在必须使用像

这样的switch case语句
switch (total) {
    case 0: // all options negative
    case 1: // only loc is positive
    case 2: // only cat is positive
    case 3: // both loc and cat is positive
    ect
}

case中的值表示total中存在的二进制序列的整数值。需要注意的是,记录代码非常重要,特别是案例块,以便其他读者可以直接找出哪个值代表什么(就像我做的那样)。

如果每个变量有两个以上的选项,你可以使用10的因子(比如方法foo,使用(trueCond ? 1 : 0) * Math.pow(10, level)

答案 4 :(得分:0)

由于你的3个条件是固定的,你可以先让它们,然后是其他的,可以转换成switch-case。

if(price > 0 || jsBed <= bedroom || jsBuilt >= built) {
    var locCheck = (loc > 0) ? 1 : 0;
    var catCheck = (cat > 0) ? 1 : 0;
    switch(locCheck + catCheck){
        case 0:
            break;
        case 1:
            break;
        case 2:
            break;
        default:
            break;
    }
}