英文:
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 <div>
if tax: true
in any of the cartItem
's. tax
is a prop inside of productDetails
which comes from cartItem
:
const Content = (props) => {
let cartItem = props.cartItem;
{cartItem.indexOf(props.productDetails?.tax) > -1 && (
<div>
Show if Tax
</div>
)}
};
export default Content;
答案1
得分: 0
我可以帮你翻译代码部分:
所提供的代码中的条件正在检查`props.productDetails.tax`的值是否存在于`cartItem`数组中。然而,如果你想仅在任何`cartItem productDetails`中的`tax: true`时显示`<div>`,你需要检查`cartItem`数组中每个`productDetails`对象的税值。
你可以使用`Array.some()`方法来实现,该方法在数组中至少有一个元素满足回调函数中指定的条件时返回true。
const Content = (props) => {
let cartItem = props.cartItem;
if (cartItem.some((item) => item.productDetails?.tax === true)) {
return (
<div>
显示税
</div>
);
} else {
return null;
}
};
export default Content;
如果你仍感到困惑,我可以为你提供props
和productDetails
的数据。
英文:
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 <div>
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) => {
let cartItem = props.cartItem;
if (cartItem.some((item) => item.productDetails?.tax === true)) {
return (
<div>
Show if Tax
</div>
);
} 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
- 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;
- 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;
英文:
-
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) => { let cartItem = props.cartItem; return ( {cartItem.filter(p=>p?.tax).map(d => ( <div> Show if Tax </div> )))} }; export default Content;
-
f you need to show div if any of the cartItem have a tax
const Content = (props) => { let cartItem = props.cartItem; return ( {cartItem.findIndex(p=>p?.productDetails?.tax) !== -1 ? <div> Show if Tax </div> : <></> )} }; export default Content
;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论