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

245
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
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