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

124
Vistas
how to replace a single key value pair from an object in react.js

I have a single object of with key value pair, and i need to replace the first key value pair with a new key value in react.js. I have tried to make immutable process like

const data = {
   firstKey : firstValue,
   secondKey : secondValue,
   thirdKey : thirdValue
}

const newData = {...data, newFirstKey: newFirstValue}

but its adding a new key value pair to the object

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

0

That is expected behavior. See MDN docs on spreading Objects.

If you want to replace a value you have to use the property name which you want to replace and set the new value.

const data = {
   firstKey : "firstValue",
   secondKey : "secondValue",
   thirdKey : "thirdValue"
}

const newData = {...data, firstKey: "newFirstValue"}
console.log(newData)

If you use a new unknown property name, this new property will be added to the object.

const data = {
   firstKey : "firstValue",
   secondKey : "secondValue",
   thirdKey : "thirdValue"
}

const newData = {...data, unknownProperty: "newFirstValue"}
console.log(newData)

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