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;
2
Answers
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 iftax: true
in any of thecartItem productDetails
, you need to check the tax value of eachproductDetails
object in thecartItem
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.If you’re still confused then I want you to provide the data of props and productDetails
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
f you need to show div if any of the cartItem have a tax
;