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

247
Visualizações
How to convert if else shorthand to normal if else case

Below code, I update the react hook with if-else shorthand condition. To understand exactly this, I try to write it in normal if-else condition, but I couldn't. What is the equal case of this shorthand conditions?

This is working code.

const [array, setArray] = useState([]);

function addProduct(product){
  const ProductExist = array.find((item) => item.id === product.id);
  setArray(
   array.map((item) =>
   item.id === product.id
   ? { ...ProductExist, quantity: ProductExist.quantity + 1 }
   : item
   )
 );
}

This is my try according to the solutions of other topics. But it gives error.

setTestArray(array.map((item) => {
  return if(item.id === product.id){
   return { ...ProductExist, quantity: ProductExist.quantity + 1 }
  }else{
   return item
  }
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

  setArray(
   array.map((item) =>
     if(item.id === product.id){
       return { ...ProductExist, quantity: ProductExist.quantity + 1 };
     } else {
       return item;
     }
   )

about 4 years ago · Juan Pablo Isaza Relatório

0

You have an extra return

It should be like this:


setTestArray(array.map((item) => {
  if(item.id === product.id){
   return { ...ProductExist, quantity: ProductExist.quantity + 1 }
  }else{
   return item
  }
}))

about 4 years ago · Juan Pablo Isaza Relatório
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