Answer the question
In order to leave comments, you need to log in
How to check if an object is in an array in JSX?
There is a page with products that can be added to the cart, cart.
cart - a separate state array with objects to which the selected products are added.
I want to make a condition under which the presence of a product in the cart would be checked, and if it is there, then the corresponding phrase would be displayed on the product page.
The condition is this. It checks if there is an item in the array with the corresponding id, and if so, a certain label is rendered.
<div>
{
cart.find(item => (item.id === id) ?
<p>I am in</p>
:
<p>I am not in</p>
)
}
</div>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question