简体   繁体   English

将多个if-else语句转换为对象的大小写转换

[英]Convert multiple if-else statement into switch-case for objects

I have an object in which I have to verify different values using multiple if-else statements, like below is sample javascript snippet: 我有一个对象,我必须在其中使用多个if-else语句来验证不同的值,例如以下是示例javascript代码段:

if(properties.values.a > 0 ){

}else if(properties.values.b > 0){

}else if(properties.values.c > 0){

}else if(properties.values.d > 0){

}else if(properties.values.e > 0){

}else{

}

I am willing to replace this multiple if-else statement into switch-case. 我愿意将此多个if-else语句替换为switch-case。 Now I wonder if that is possible for objects or not? 现在我想知道对象是否可行? If possible, how should be going towards it? 如果可能,应该如何发展?

Switch is not supposed to work like this. 开关不应该这样工作。 So, no, this is not supported. 因此,不,不支持此功能。 As a workaround you could do this 作为解决方法,您可以执行此操作

switch (true) {
    case (properties.values.a > 0):
        ...
    case (properties.values.b > 0):
        ...
    case (properties.values.c > 0):
        ...
    case (properties.values.d > 0):
        ...
    case (properties.values.e > 0):
        ...
}

Still, is pretty ugly so i suggest you stick to if/else. 尽管如此,还是很难看的,所以我建议您坚持使用if / else。

You can do that with a for in loop: 您可以使用for in循环来实现:

 o = { a:0, b:0, c:1, d:0 }; for(var p in o) { if (o[p] > 0) { //... console.log(o[p]); break; } } 

You could use an array with the wanted keys and use iterate with a short circuit if a value is found. 您可以使用带有所需键的数组,并在找到值的情况下进行短路迭代。

If no one is found call callOther . 如果找不到任何人,请致电callOther

['a', 'b', 'c', 'd', 'e'].some(k => {
    if (properties.values[k] > 0) {
        callThis();
        return true;
    }
}) || callOther();

In JavaScript you can only use switch to check for equality. 在JavaScript中,您只能使用switch来检查是否相等。 You can't check whether a > b using a switch. 您无法使用开关检查a > b

If you want a more elegant solution, that I particularly would only use if I needed to check a huge amount of rules, is to do something like this: 如果您想要一个更优雅的解决方案,那么我特别需要检查大量规则时才使用该解决方案:

const checkers = [];
checkers.push([p => p.values.a > 0, () => { /* do something */ }])
checkers.push([p => p.values.b > 0, () => { /* do something */ }])
...
checkers.filter(checker => checker[0](property)).forEach(checker => checker[1]())

In the above example, checkers is an array of arrays in which the first element is a predicate function and the second is a function that should execute if the predicate is true. 在上面的示例中,检查器是一个数组数组,其中第一个元素是谓词函数,第二个元素是在谓词为true时应执行的函数。

But again... For most cases you just do an if/else as you described. 但是,再说一次...在大多数情况下,您只需按照说明进行if/else

Also, if you want to check if a value is greater than 0, you might use just if(a) , because if a is not 0 , then it's truthy, otherwise it's falsy. 另外,如果您要检查值是否大于0,则可以只使用if(a) ,因为如果a不为0 ,那么它是真实的,否则是虚假的。

EDIT 编辑

Apparently, technically, you can use logical expressions within switch statements, even though it seems hacky =/ 显然,从技术上讲,即使看起来很笨拙,您也可以在switch语句中使用逻辑表达式= /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM