Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda