【问题标题】:How to form a Ternary for 3 with 1 constant如何用 1 个常数形成 3 的三进制
【发布时间】:2019-03-20 06:16:11
【问题描述】:

编辑到完整场景:我有两个组件和四个按钮!三个按钮的默认值为null。假设 abcd 是四个不同的按钮。

  • 在第一个组件中,按钮 ab 默认为 null,因此按钮 d 将被禁用。
  • 当用户按下 ab 时,会为 ab 分配一个值>,所以在 a & b 被赋值之后,应该启用 d

  • 在第二个组件中,按钮 ac 将具有默认值 null,并且当它们都获得值 onPressing 时,再次按钮 d 将被启用。

请注意,组件是有条件地呈现的,因此用户只能看到一个组件!

这是我的代码:

 <Button
     disabled={(this.state.quantityItemSelected === null || 
     this.state.deliveryOptionSelected === null) && 
     (this.state.timeSlotItemSelected === null) ? true : false} 
 />

默认常量是this.state.quantityItemSelected === null,两者相同。

它在() 中的条件下工作正常,但关于第一个,当this.state.quantityItemSelected 首先变成不是null 然后this.state.timeSlotItemSelected 变成不是null 它工作正常,

但是当我反转它时,当this.state.timeSlotItemSelected 起初不为空时,条件变为假。

我的预期工作是,当任何东西都是null 时,它应该是真的。

请帮助解决这个问题,让我知道还有什么需要了解的

【问题讨论】:

  • 值是否为假,是否始终为null
  • 这太令人困惑了。但是您不需要使用三元运算符。只需使用&amp;&amp;|| 即可满足条件。
  • 不,实际上,当用户按下相应的 !因此,两者都有一个相同的常数,因此当该常数和其他条件为空时,当两者都变为非空时,它必须为真,那么它应该为假!
  • 你能做一个stackblitz.com 的例子吗?

标签: javascript conditional-statements ternary-operator


【解决方案1】:

如果当 a = null 且 b = null 时我有 a,b,c 那么它 应该是真的,当 a = null 和 c = null 它应该是真的,休息 条件为假。

试试下面的,它会工作的!你可以用这个。

(this.state.quantityItemSelected === null || this.state.deliveryOptionSelected === 
 null) && (this.state.timeSlotItemSelected === null || this.state.quantityItemSelected 
=== null) ? true : false

如果有效,请告诉我!

【讨论】:

    【解决方案2】:

    例如 - 如果我有 a,b,c 当 a = null 和 b = null 时它应该为真,当 a = null 和 c = null 时它应该为真,其余条件为假。

    您需要将后两个条件括起来并使用||

     <Button
       disabled={(
         (this.state.quantityItemSelected === null) &&
         (this.state.deliveryOptionSelected === null || this.state.timeSlotItemSelected === null)
       ) ? true : false} 
     />
    

    【讨论】:

    • 对这个问题有任何想法吗?
    【解决方案3】:

    例如 - 如果我有 a,b,c 当 a = null 和 b = null 时它应该为真,当 a = null 和 c = null 时它应该为真,其余条件为假。

    你(在伪逻辑符号中)是(!a &amp;&amp; !b &amp;&amp; c) || (!a &amp;&amp; b &amp;&amp; !c),扩展意味着

    function calculateConditions(a, b, c) {
      return (a === null && b === null && c !== null) || (a === null && b !== null && c === null);
    }
    
    //T - true, F - False. Shortenned to keep the formatting
    const table = [
      ["FFF", calculateConditions( null     ,  null     ,  null)],
      ["FFT", calculateConditions( null     ,  null     , "not null")],
      ["FTF", calculateConditions( null     , "not null",  null)],
      ["FTT", calculateConditions( null     , "not null", "not null")],
      ["TFF", calculateConditions("not null",  null     ,  null)],
      ["TFT", calculateConditions("not null",  null     , "not null")],
      ["TTF", calculateConditions("not null", "not null",  null)],
      ["TTT", calculateConditions("not null", "not null", "not null")],
    ]
    
    for (const [key, value] of table) {
      console.log(key, value)
    }

    【讨论】:

    • 对这个问题有任何想法吗?
    【解决方案4】:

    假设'this.state.quantityItemSelected'是'a','this.state.deliveryOptionSelected'是'b','this.state.timeSlotItemSelected'是'c',条件应该是:

    this.state.quantityItemSelected === null && 
    (this.state.deliveryOptionSelected === null ||
    this.state.timeSlotItemSelected === null) ? true : false
    

    或与 a、b 和 c

    a === null && (b === null || c === null)  ? true : false
    

    如果零、空字符串等虚假值不是 a、b 和 c 的有效值,您可以通过以下方式改进代码:

    disable = !a && (!b || !c)
    

    回答已编辑的问题

    在第一个组件中,按钮 a 和 b 默认为 null,因此按钮 d 将被禁用。 当用户按下 a & b 时,会为 a & b 分配一个值,因此在分配 a & b 后,应启用 d。

    这是与前一个不同的场景。

    disableButtonD = a === null && b === null;  
    

    或更好

    disableButtonD = !(a && b);  
    

    对于:

    在第二个组件中,按钮 a 和 c 的默认值为 null,当它们都获得 onPressing 值时,按钮 d 将再次启用。

    disableButtonD = a === null && C === null;  
    

    或更好

    disableButtonD = !(a && C);  
    

    根据您的限制,您不能在两种情况下都使用一个句子。 当然,根据你使用的框架,你可以有一个函数。

    角度示例:

    组件:

    disableD(firstValue, secondValue) {
      return !(firstValue && secondValue)
    } 
    

    HTML

    <!-- first Button -->
    <button [disabled]="disableD(a, b)"></button>
    
    <!-- second button-->
    <button [disabled]="disableD(a, c)"></button>
    

    【讨论】:

    • 实际上,它也不起作用!因为,现在如果 quantityItemSelected 或 timeSlotItemSelected 不为空,它就变为 true !
    • 对这个问题有任何想法吗?
    猜你喜欢
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    • 2012-03-15
    • 2021-11-09
    • 2017-09-09
    • 1970-01-01
    相关资源
    最近更新 更多