Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

207
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda