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

142
Vistas
Confusion regarding Spread Operator in JS

So basically I have list of objects. I am iterating and have to change one property of the object. I have two scenarios, in one I have to change the property of a particular object.

 setList(list.map((list_item)=>
      list_item.id===id?{...list_item,doubleClick:true}:list_item
    )

And there is another scenario where I have to change the property of every object in the list.

setList(list.map((list_item)=>{...list_item,doubleClick:false}))

The first case is working fine but the second case is throwing error >Parsing error: Unexpected token (34:35)

To me they don't look that different except first one has ternary operator? Is that what's making a difference? How to fix the second case?

Edit: So I think spread operator works with an expression or declaration.

setList(list.map((list_item)=>list_item={...list_item,doubleClick:false}))

This works. Is there any better or correct way to do it?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

In your second case, you need to return the new object.

setList(
  list.map((list_item) => {
    return { ...list_item, doubleClick: false };
  })
);

Or a shorter way is to wrap the new object inside ()

setList(list.map((list_item) => ({ ...list_item, doubleClick: false })));

Read more about the syntax here

Edit:

The syntax in the third case is fine but the logic isn't correct. You want to return the new object, not reassign elements in .map()

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