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

188
Vistas
I have to edit only one property in a JS object, can I spread only a portion of a said object when passing it as an argument to a function?

I have a JavaScript object with the following structure:

{
    id: 0,
    content: "English Class",
    date: "Dec 24th at 7:30pm",
    completed: false,
}

I also have a function const editTask(id, content) that updates the content property in the aforementioned object:

const editTask = (id, content) => {
    setTasks(tasks.map((task) =>
      task.id === id ? { id: task.id, content: content, date: task.date, completed: task.completed } : task));
  }

Is there a way in which I can avoid passing all the object properties to only upgrade the content property as well as just spreading the rest of the object?

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

0

A common pattern is to combine the spread syntax with manually setting some properties:

const editTask = (id, content) => {
    setTasks(tasks.map((task) =>
      task.id === id ? { ...task, content } : task));
  }

In the above example, the spread syntax sets all the properties of task, and the next line overrides the property content to the new value. It's important that the spread is before the content. If it were after, then the new value would be overridden by the spread.

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