Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!