Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda