将嵌套的if语句转换为更优雅的开关?

时间:2011-06-18 02:47:07

标签: javascript if-statement

如何将这个嵌套的if语句转换为更易读和更优雅的东西?

if(speed==0){
            othvalue=0;
        }else {
            if(speed>value1864cmn){
                othvalue=value1864cmn;
            }else {
                if(speed>value1746cmn){
                    othvalue=value1746cmn;
                }else {
                    if(speed>value1628cmn){
                        othvalue=value1628cmn;
                    }else {
                        if(speed>value1510cmn){
                            othvalue=value1510cmn;
                        }else {
                            if(speed>value1392cmn){
                                othvalue=value1392cmn;
                            }else {
                                if(speed>value1274cmn){
                                    othvalue=value1274cmn;
                                }else {
                                    if(speed>value1156cmn){
                                        othvalue=value1156cmn;
                                    }else {
                                        if(speed>value1038cmn){
                                            othvalue=value1038cmn;
                                        }else {
                                            if(speed>value920cmn){
                                                othvalue=value920cmn;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        };

5 个答案:

答案 0 :(得分:5)

您可以使用“匹配”构造:

var othvalue = (speed == 0) ? 0 
             : (speed <= 10) ? 10
             : (speed <= 20) ? 20
             : (speed <= 30) ? 30
             : 40;

答案 1 :(得分:3)

您不需要嵌套它,也不需要将其转换为switch语句。只需将if放在else旁边。

例如:

if(speed==0){
    othvalue=0;
} else if(speed>value1864cmn){
    othvalue=value1864cmn;
} else if {
...

答案 2 :(得分:1)

if (speed == 0) {
    othvalue = 0;
} else if (speed > value1864cmn) {
    othvalue = value1864cmn;
} else if (speed > value1746cmn) {
    othvalue = value1746cmn;
} else if (speed > value1628cmn) {
    othvalue = value1628cmn;
} else if (speed > value1510cmn) {
    othvalue = value1510cmn;
} else if (speed > value1392cmn) {
    othvalue = value1392cmn;
} else if (speed > value1274cmn) {
    othvalue = value1274cmn;
} else if (speed > value1156cmn) {
    othvalue = value1156cmn;
} else if (speed > value1038cmn) {
    othvalue = value1038cmn;
} else if (speed > value920cmn) {
    othvalue = value920cmn;
};

答案 3 :(得分:1)

if (speed == 0) {
    othvalue = 0;
} else {
    var values = [
        value1746cmn, value1628cmn, value1510cmn, value1392cmn, value1274cmn,
        value1156cmn, value1038cmn, value920cmn
    ];
    for (var i in values) {
        if (speed > values[i]) {
            othvalue = values[i];
            break;
        }
    }
}

答案 4 :(得分:1)

//我不知道优雅,但是通过一个数组非常快。

function setSpeedValue(speed){
    var L= 9, values= [0, 920, 1038, 1156, 1274, 1392, 1510, 1628, 1746, 1864];
    while(values[L]>speed)--L;
    return 'value'+values[L]+'cmn';
}
alert(setSpeedValue(1040))