如何在一个默认条件下对 3 个条件执行三元语句
How to perform Ternary statement for 3 conditions with one default condition
是否可以对 3 个条件执行三元运算但有一个默认条件?
这是我的代码:
<Button
rounded={true}
title='Add Address'
backgroundColor='#2980b9'
rightIcon={{name: 'arrow-forward'}}
disabled={this.state.timeSlotItemSelected === null || this.state.quantityItemSelected === null ? true : false}
/>
这里默认是this.state.timeSlotItemSelected
。我需要的是,每当 this.state.timeSlotItemSelected
和 this.state.quantityItemSelected
为空时,结果应为 true
,而每当 this.state.timeSlotItemSelected
和 this.state.deliveryOptionSelected
为空时,结果也应为是 true
。否则应该是 false
.
如何针对这两种情况执行此操作?
您可以使用 &&
和 ||
的简单组合来完成此操作,根本不需要三元运算符。
this.state.timeSlotItemSelected === null &&
(this.state.quantityItemSelected === null || this.state.deliveryOptionSelected === null)
如果 this.state.timeSlotItemSelected
为空,并且 this.state.quantityItemSelected
或 this.state.deliveryOptionSelected
之一为空,则为 true
,否则为 false
。
真相table:
timeSlotItemSelected | quantityItemSelected | deliveryOptionSelected | Result
---------------------+----------------------+------------------------+-------
null | null | null | true
null | <not null> | null | true
null | null | <not null> | true
null | <not null> | <not null> | false
<not null> | <anything> | <anything> | false
在这种情况下您不需要三元运算符,因为表达式本身将 return true
或 false
: <Expression> ? true : false
= <Expression>
。
(this.state.timeSlotItemSelected === null && this.state.quantityItemSelected === null) || ( this.state.deliveryOptionSelected === null && this.state.timeSlotItemSelected === null) ?真 : 假
这是一些布尔代数(我可能会用错词,我是用德语学数学的,所以欢迎编辑或评论):
让我们从翻译成Java脚本的口头条件开始(为了更好的可读性,我删除了this.state
):
(timeSlotItemSelected === null && quantityItemSelected === null) || (timeSlotItemSelected === null && deliveryOptionSelected === null)
我们可以分解出 timeSlotItemSelected
,所以它看起来像这样:
timeSlotItemSelected === null && (quantityItemSelected === null || deliveryOptionSelected === null)
现在您可以在 if 子句或三元运算符中使用它。
关于 "default condition" 的问题:
在使用布尔值时,实际上没有办法设置默认值,它们不是真就是假。
这意味着:
如果 if 子句或三元运算符有一个布尔值作为输入(它们在 JavaScript 和大多数其他语言中都有),它们不能有默认的第三种情况,因为布尔值只能是真或假.
布尔类型的变量可以有默认值(例如 Java 中的 false),但 JavaScript 中没有。
是否可以对 3 个条件执行三元运算但有一个默认条件?
这是我的代码:
<Button
rounded={true}
title='Add Address'
backgroundColor='#2980b9'
rightIcon={{name: 'arrow-forward'}}
disabled={this.state.timeSlotItemSelected === null || this.state.quantityItemSelected === null ? true : false}
/>
这里默认是this.state.timeSlotItemSelected
。我需要的是,每当 this.state.timeSlotItemSelected
和 this.state.quantityItemSelected
为空时,结果应为 true
,而每当 this.state.timeSlotItemSelected
和 this.state.deliveryOptionSelected
为空时,结果也应为是 true
。否则应该是 false
.
如何针对这两种情况执行此操作?
您可以使用 &&
和 ||
的简单组合来完成此操作,根本不需要三元运算符。
this.state.timeSlotItemSelected === null &&
(this.state.quantityItemSelected === null || this.state.deliveryOptionSelected === null)
如果 this.state.timeSlotItemSelected
为空,并且 this.state.quantityItemSelected
或 this.state.deliveryOptionSelected
之一为空,则为 true
,否则为 false
。
真相table:
timeSlotItemSelected | quantityItemSelected | deliveryOptionSelected | Result
---------------------+----------------------+------------------------+-------
null | null | null | true
null | <not null> | null | true
null | null | <not null> | true
null | <not null> | <not null> | false
<not null> | <anything> | <anything> | false
在这种情况下您不需要三元运算符,因为表达式本身将 return true
或 false
: <Expression> ? true : false
= <Expression>
。
(this.state.timeSlotItemSelected === null && this.state.quantityItemSelected === null) || ( this.state.deliveryOptionSelected === null && this.state.timeSlotItemSelected === null) ?真 : 假
这是一些布尔代数(我可能会用错词,我是用德语学数学的,所以欢迎编辑或评论):
让我们从翻译成Java脚本的口头条件开始(为了更好的可读性,我删除了this.state
):
(timeSlotItemSelected === null && quantityItemSelected === null) || (timeSlotItemSelected === null && deliveryOptionSelected === null)
我们可以分解出 timeSlotItemSelected
,所以它看起来像这样:
timeSlotItemSelected === null && (quantityItemSelected === null || deliveryOptionSelected === null)
现在您可以在 if 子句或三元运算符中使用它。
关于 "default condition" 的问题:
在使用布尔值时,实际上没有办法设置默认值,它们不是真就是假。
这意味着: 如果 if 子句或三元运算符有一个布尔值作为输入(它们在 JavaScript 和大多数其他语言中都有),它们不能有默认的第三种情况,因为布尔值只能是真或假.
布尔类型的变量可以有默认值(例如 Java 中的 false),但 JavaScript 中没有。