Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
if else condition not working properly in map function

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);
                  }
                 })
                }
              });
          }
        }
      }
    }); 
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!