I am trying to make a website where you can "buy" books. (It is just a mock project) There is a catalog where you see the books and you can add them to a shopping cart.
On the catalog side, the books are stored in a books array.
books =[{
"name":"bible",
"author": "god"},
{
"name":"javascript",
"author": "web"}];
When the button "add to the shopping cart" is clicked a function is called with the specific book name(e.g. bible). Then the book is added to the shopping cart array. shoppingCart =[];
Before the values are added to the shopping cart array I want to check if a book with the same name already exists in the shopping cart. If yes a counter should just count +1. If not the whole book should be added.
I do not understand how to check if a value already exists in an array. I tried to check with
addToShoppingCart(name) {
this.shoppingCart.name.indexOf(name),
this.shoppingCart.indexOf(name),
this.shoppingCart.includes(name),
this.shoppingCart.name.inclueds(name)}
but nothing of that did work. Can anyone please help me with that task?
this.shoppingCart.find(item => item.name === name) !== undefinedlet isExist = !!this.shoppingCart.filter( b => b.name === name).length