【问题标题】:How to perform Ternary statement for 3 conditions with one default condition如何使用一个默认条件执行 3 个条件的三元语句
【发布时间】:2019-06-06 08:15:01
【问题描述】:

是否可以对 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.timeSlotItemSelectedthis.state.quantityItemSelected 为空时,结果应该是true,并且只要this.state.timeSlotItemSelectedthis.state.deliveryOptionSelected 为空,那么它也应该是true。否则应该是false

如何在这两种情况下执行此操作?

【问题讨论】:

  • 你真的要检查null吗?你还有什么价值观?

标签: javascript react-native ternary-operator


【解决方案1】:

这是一些布尔代数(我可能会用错词,我是用德语学习数学的,因此感谢编辑或 cmets):

让我们从将您的口头条件翻译成 JavaScript 开始(我删除了 this.state 以提高可读性):

(timeSlotItemSelected === null &amp;&amp; quantityItemSelected === null) || (timeSlotItemSelected === null &amp;&amp; deliveryOptionSelected === null)

我们可以分解出timeSlotItemSelected,所以它看起来像这样:

timeSlotItemSelected === null &amp;&amp; (quantityItemSelected === null || deliveryOptionSelected === null)

现在您可以在 if 子句或三元运算符中使用它。

关于“默认条件”的问题:

在使用布尔值时实际上没有办法有默认值,它们要么是真要么是假。

这意味着: 如果 if 子句或三元运算符有一个布尔值作为输入(它们在 JavaScript 和大多数其他语言中都有),它们不能有第三种情况作为默认值,因为布尔值只能是真或假。

boolean 类型的变量可以有默认值(例如 Java 中的 false),但 JavaScript 中没有。

【讨论】:

  • 我想知道“默认条件”是否意味着一个条件适用于两个测试用例。所以对于D = default,我们有D ∧ A =&gt; X ; D ∧ B =&gt; Y ; else =&gt; Z,其中默认情况被分解为;类似D ∧ (A =&gt; X ; B =&gt; Y) ; else =&gt; Z。在 Lisp/Scheme 中,您可以使用 (cond-default D (A X ...) (B Y ...) (else Z ...))
  • 感谢您的回复,很高兴它完美运行@Daniel
  • 你是对的@BillyBrown,我提到了某种意义上的默认值,第一个条件适用于这两种情况!
【解决方案2】:

(this.state.timeSlotItemSelected === null && this.state.quantityItemSelected === null) || (this.state.deliveryOptionSelected === null && this.state.timeSlotItemSelected === null)?真:假

【讨论】:

    【解决方案3】:

    您可以通过&amp;&amp;|| 的简单组合来做到这一点,根本不需要三元运算符。

    this.state.timeSlotItemSelected === null &&
        (this.state.quantityItemSelected === null || this.state.deliveryOptionSelected === null)
    

    如果this.state.timeSlotItemSelected 为空,如果this.state.quantityItemSelectedthis.state.deliveryOptionSelected 之一为空,则为true,否则为false

    真值表:

    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
    

    在这种情况下您不需要三元运算符,因为表达式本身将返回 truefalse&lt;Expression&gt; ? true : false = &lt;Expression&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-06
      • 2013-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      相关资源
      最近更新 更多