I am adding the items into shopcart array if the item quantity is greater than cart item quantity or empty. After its first entry the item won't add it again if the id is matched to shopcart item id. I used map function and use the if condition but it's not working.
Problem is : Suppose I am adding item id 1 at first hit, its adding into shop cart array. And then how many I add to cart , the item is not adding again into the shopcart array. If I add different item then it added to the shopcart array. Everything is fine until now.
But if I click again any of it though it exists into the shop cart array but it added to the shopcart array and doesn't care the condition.
Code:
addToCart(itemtoAdd){
this.itemList.map((item,index)=>{
if(item.id ==itemtoAdd.id){
if(item.quantity> 0 ){
if(this.shopcart.length === 0){
item.quantity = item.quantity -1;
this.shopcart.push(itemtoAdd);
console.log(this.shopcart)
this.cartCount = 1;
}else{
this.shopcart.map((shopitem)=>{
if(shopitem.id==itemtoAdd.id){
item.quantity = item.quantity -1;
this.cartCount = this.cartCount+1;
console.log(this.shopcart);
}else{
this.shopcart.map((shopitem)=>{
if(shopitem.id!== itemtoAdd.id){
item.quantity = item.quantity -1;
this.shopcart.push(itemtoAdd);
this.cartCount = this.cartCount+1;
console.log(this.shopcart);
}
})
}
});
}
}
}
});
}