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

查看:113
本文介绍了逻辑运算符两侧的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
}

到目前为止,没有意外的结果。
但是,在替换||时遵循类似的结构&& amp;的操作员操作员,事情并没有像我期望的那样完成。

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]

推荐答案

逻辑OR 运算符无法正常运行正在寻找。

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


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

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 方法。请注意,它将返回数组元素的索引,因此 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) ) { ... }

你可以做的另一件事就是在 c>运营商。另外,因为它只检查,你可以将留空,如下所示:

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天全站免登陆