简体   繁体   English

如何在两个条件下,其中一个可以是多个值中的一个

[英]How to if on two conditions where one can be either of several values

I want to check that:我想检查一下:

  1. if the state is true and the connection type is not 2 or 6 then do something.如果状态为真且连接类型不是 2 或 6,则执行某些操作。

  2. if state is false and connection type is either 2 or 6 then do something else.如果 state 为 false 且连接类型为 2 或 6,则执行其他操作。

I have the following if statements but at points it is using the wrong code:我有以下 if 语句,但有时它使用了错误的代码:

async setWiFiConnection(state){
        try{
            var connType = await driver.getNetworkConnection();
            if(state == true && connType != 2 && connType != 6){
                await driver.toggleWiFi();
            }else if(state == false && connType == 2 || connType == 6){
                await driver.toggleWiFi();
            }
        }catch(err){
            console.log(err);
        } 
    }

I think the issue is if the connection type is 6 even if state is true then the else if code is being executed.我认为问题是如果连接类型为 6,即使 state 为真,则 else if 代码正在执行。

Dave Newton's answer was what I was after. Dave Newton 的回答正是我所追求的。 I needed to stick the connType checks in brackets see below:我需要将 connType 检查粘贴在括号中,如下所示:

async setWiFiConnection(state){
        try{
            var connType = await driver.getNetworkConnection();
            if(state == true && connType != 2 && connType != 6){
                await driver.toggleWiFi();
            }else if(state == false && (connType == 2 || connType == 6)){
                await driver.toggleWiFi();
            }
        }catch(err){
            console.log(err);
        } 
    }

Thanks Dave谢谢戴夫

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

相关问题 Nodejs - 如何在两个条件下找到索引,一个条件是属性存在的地方 - Nodejs - How to find the index with two conditions, one condition being where a property exists 正则表达式:匹配两个条件之一? - Regular expression : match either of two conditions? 如何测试变量是否不等于两个值中的任何一个? - How do I test if a variable does not equal either of two values? 如何将包含多个对象的两个或多个数组合并为一个并增加其ID? - How can I merge two or more arrays containing several objects into one and increment their IDs? 检查两个值是否为负 - Checking if either two values is negative 正则表达式匹配两者之一 - Regex match either one of the two 如何通过许多不同的类别列表对数据项列表进行分类,其中每个列表包含几个不同的类别值? - How does one categorize a list of data items via many different category lists where each list contains several distinct category values? 如何为一个指令创建的多个输入字段分配不同的ng-model值 - How can I assign different ng-model values to several input fields created from one directive 两个条件合二为一 - Two conditions into a single one 如何查询 Firebase firestore 数组中的两个值,其中一个值在另一个值之后编入索引? - How to query two values inside an array in Firebase firestore, where one value is indexed after the other one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM