I am working on my store project using JS. When I click on the add-to-cart button, an array with objects – image, name, price, is added to my storage. But, I can add such arrays, products, infinitely to the cart storage. How can I prevent adding the same product to the cart?
addToCard = (e) => {
if (e.target.closest(".add-to-cart")) {
let price = e.target.parentElement.parentElement.dataset.price;
let name = e.target.parentElement.parentElement.parentElement.dataset.name;
let img = e.target.parentElement.parentElement.parentElement.dataset.img;
const cartItems = { price, name, img };
apiService.post("/order", cartItems).then(() => {
this.setState({
...this.state,
orderCart: this.state.orderCart?.concat(cartItems),
})
})
useToastNotification({
message: "Product in the cart!",
type: TOAST_TYPE.success,
});
}
};
I want to add an item to the cart, only once. My items have different names. How can I check the variable name in the array and not add the product with that name to the storage?

