只有在数组中定义了 prop 时才显示

huangapple go评论47阅读模式
英文:

Show <div> only if prop is defined in array

问题

我只想在任何一个`cartItem`中的`tax: true`时显示`<div>``tax``productDetails`内部的一个属性来自于`cartItem`

        const Content = (props) => { 

        let cartItem = props.cartItem;

            {cartItem.indexOf(props.productDetails?.tax) > -1 && (
                <div>
                    显示税
                </div>
              )}
    
        };
        export default Content;
英文:

I only want to show the &lt;div&gt; if tax: true in any of the cartItem's. tax is a prop inside of productDetails which comes from cartItem:

只有在数组中定义了 prop 时才显示 <div>。

    const Content = (props) =&gt; { 

    let cartItem = props.cartItem;

        {cartItem.indexOf(props.productDetails?.tax) &gt; -1 &amp;&amp; (
            &lt;div&gt;
                Show if Tax
            &lt;/div&gt;
          )}

    };
    export default Content;

答案1

得分: 0

我可以帮你翻译代码部分:

所提供的代码中的条件正在检查`props.productDetails.tax`的值是否存在于`cartItem`数组中然而如果你想仅在任何`cartItem productDetails`中的`tax: true`时显示`&lt;div&gt;`你需要检查`cartItem`数组中每个`productDetails`对象的税值

你可以使用`Array.some()`方法来实现该方法在数组中至少有一个元素满足回调函数中指定的条件时返回true

const Content = (props) => {
  let cartItem = props.cartItem;

  if (cartItem.some((item) => item.productDetails?.tax === true)) {
    return (
      &lt;div&gt;
        显示税
      &lt;/div&gt;
    );
  } else {
    return null;
  }
};

export default Content;

如果你仍感到困惑,我可以为你提供propsproductDetails的数据。

英文:

The condition in the code you provided is checking if the value of props.productDetails.tax exists in the cartItem array. However, if you want to show the &lt;div&gt; only if tax: true in any of the cartItem productDetails, you need to check the tax value of each productDetails object in the cartItem array.

You can do this using the Array.some() method, which returns true if at least one element in the array satisfies the condition specified in the callback function.

const Content = (props) =&gt; {
  let cartItem = props.cartItem;

  if (cartItem.some((item) =&gt; item.productDetails?.tax === true)) {
    return (
      &lt;div&gt;
        Show if Tax
      &lt;/div&gt;
    );
  } else {
    return null;
  }
};

export default Content;

If you're still confused then I want you to provide the data of props and productDetails

答案2

得分: 0

  1. Cart Item is an array so you will have multiple cartItems there need to loop each and display div for all cartItems which have a tax
const Content = (props) => { 
 let cartItem = props.cartItem;
 return (
     {cartItem.filter(p => p?.tax).map(d => (
         <div>
             显示如果有税
         </div>
     ))}
 );
};
export default Content;
  1. If you need to show a div if any of the cartItems have a tax
const Content = (props) => { 
 let cartItem = props.cartItem;
 return (
     {cartItem.findIndex(p => p?.productDetails?.tax) !== -1 ?
         <div>
             显示如果有税
         </div>
     : <></> }
 );
};
export default Content;
英文:
  1. Cart Item is an array so you will have multiple cartItems there need to loop each and display div for all cartItems which hax a tax

    const Content = (props) =&gt; { 
     let cartItem = props.cartItem;
     return (
     {cartItem.filter(p=&gt;p?.tax).map(d =&gt; (
            &lt;div&gt;
                Show if Tax
            &lt;/div&gt;
          )))}
     };
     export default Content;
    
  2. f you need to show div if any of the cartItem have a tax

    const Content = (props) =&gt; { 
    
      let cartItem = props.cartItem;
      return (
      {cartItem.findIndex(p=&gt;p?.productDetails?.tax) !== -1 ?
         &lt;div&gt;
             Show if Tax
         &lt;/div&gt;
      : &lt;&gt;&lt;/&gt; )}
    };
     export default Content
    

;

huangapple
  • 本文由 发表于 2023年2月16日 02:23:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75463988.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定