逻辑运算符两边的if语句中的多个条件 [英] Multiple conditions in if statement on both sides of the logical operator

查看:48
本文介绍了逻辑运算符两边的if语句中的多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试在逻辑运算符两侧的 if 语句中使用多个参数.我首先从 || 开始运算符,按预期工作:

I was experimenting with having multiple arguments in an if statement on both sides of the logical operator. I first started with the || operator, which worked as expected:

var a = 'apple', b = 'banana', c = 'cherry';

if (a == 'banana' || a == 'apple' || b == 'banana' || b == 'apple') {
    console.log('example 1') // returns
}

if ((a || b) == 'banana' || (a || b) == 'apple') {
    console.log('example 2') // returns
}

if (a == ('apple' || 'banana') || b == ('apple' || 'banana')) {
    console.log('example 3') // returns
}

if ((a || b) == ('apple' || 'banana')) {
    console.log('example 4') // returns
}

到目前为止,没有意外的结果.但是,当替换 || 时遵循类似的结构&& 的运算符操作员,事情并不像我期望的那样工作.

So far, no unexpected results. However, when following a similar structure when replacing the || operator for the && operator, things don't quite work as I expect them to.

if ((a == 'banana' && b == 'apple') || (a == 'apple' && b == 'banana')) {
    console.log('example 5') // returns
}

if (((a || b) == 'banana') && ((a || b) == 'apple')) {
    console.log('example 6') // DOESN'T RETURN
}

if ((a || b) == 'banana') {
    console.log('example 6a') // DOESN'T RETURN - consistent with example 6
}

if ((a == ('apple' || 'banana')) && (b == ('apple' || 'banana'))) {
    console.log('example 7') // DOESN'T RETURN
}

if (a == ('apple' || 'banana')) {
    console.log('example 7a') // returns - inconsistent with example 7
}

if (b == ('apple' || 'banana')) {
    console.log('example 7b') // DOESN'T RETURN - inconsistent with example 7a
}

if ((a && b) == ('apple' || 'banana')) {
    console.log('example 8') // DOESN'T RETURN
}

if ('apple' == (a || b) && 'banana' == (a || b)) {
    console.log('example 9') // DOESN'T RETURN
}

现在,我想知道:我的逻辑是否存在缺陷,或者不能这样做?为了可读性和可维护性,我的目标是尽可能短地编写这些 if 语句.显然,我只是在探索可能性.

Now, I am wondering: is there a flaw in my logic or can it just not be done this way? My aim is to write these if statements as short as possible, for the purpose of readibility and maintainability. Clearly I am just exploring possibilities.

有谁知道如何解决这个问题?特别是示例 7/7a/7b 对我来说似乎很奇怪,因为尽管结构相似,但它产生的结果不一致 [Fiddle]

Does anyone know any way to go about this? Especially example 7/7a/7b seems peculiar to me because it yields inconsistent results despite a similar structure [Fiddle]

推荐答案

Logical OR 运算符不符合您的要求.

The Logical OR operator doesn't work in a way you're looking for.

如果可以转换为true则返回expr1;否则,返回 expr2.因此,当与布尔值一起使用时, ||如果任一操作数为真,则返回真;如果两者都为假,则返回假.

Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true; if both are false, returns false.

MDN

另一种方法是使用数组的 indexOf 方法.请注意,它将返回数组元素的 index,因此 0 也可能是一个有效值.为了使我们的 if 语句按预期工作,我们必须像这样使用 0 <= ... :

One alternative way could be make use of array's indexOf method. Just be aware it will return the index of the array element, so 0 could be a valid value also. In order to make our if statement works as expected, we have to use 0 <= ... like this:

if ( 0 <= ["apple","banana"].indexOf(a) ) { ... }

您可以做的另一件事是使用 in 运算符.此外,由于它只检查 keys,您可以将 values 留空,如下所示:

The other thing you can do is using in operator. Also as it checks only against the keys, you can leave the values empty like this:

if ( a in { "apple": "", "banana": "" } ) { ... }

如果您有很多选择,显然最好执行以下操作:

If you have lot's of options, obviously it's better to do the following:

var options = {
    "apple": "",
    "banana": ""
}

if ( a in options ) { ... }

就我个人而言,我认为只需要两个选项来检查,对于人眼进行两个单独的检查,这将更具可读性,而且我认为在您的示例中,您实际上不需要缩短 if 语句,因为在我看来它们已经很短且可读.

Personally I think with just two options to check, this will be more readable for a human-eye to go for two separated checks, and I think in your examples you don't really need to shorten the if statements as they're already quite short and readable in my opinion.

if ( "apple" === a || "banana" === a ) { ... }

这篇关于逻辑运算符两边的if语句中的多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆