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

153
Vistas
How to exclude one key/value pair from spread operator return

Let say I have an object and a function that takes one update object and return a new object with the first one and the update one like so :

const object = {
    id: 1,
    value: 'initial value'
}

const updateFunction = (updates) => {
    return {
        ...object,
        ...updates
    }
}

const updatedObject = updateFunction({
    id: 2,
    value: 'updated value'
})

Using the spread operator is there a simple way to exclude the id from spreading thus updating it knowing that I absolutely can't change my updateFunction to take multiple arguments

Edit: in the comments someone suggested a simple solution like so :

 return {...object, ...updates, id: object.id};

But let say that for some reason we don't have access to the id value of the first object, is there a way to just exclude the id key/value pair from the spreading ?

thank you

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

0

I wouldn't recommend deleting id of updates like kmoser's answer. This will directly change the object that is passed in, which is usually unexpected.

Instead, you can exclude id in a way that doesn't affect updates:

const updateFunction = (updates) => {
    const { id, ...rest } = updates;
    return {
        ...object,
        ...rest,
    }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Delete the 'id' property from 'updates' before doing the spread:

const updateFunction = (updates) => {
    delete updates['id']
    return {
        ...object,
        ...updates
    }
}

Reference: How do I remove a property from a JavaScript object?

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