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

99
Views
Changing properties in JSON data

how can I change the "ABC" field in the code below?

 var data = {ABC:{key:'dynamic_key',value:'dynamic_value'}}

Normally, I can give the value I want by saying data.ABC.key, but what I really want is to go as data.CCC instead of data.ABC, so I just want to change the ABC name.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Re-assign the inner object to the new outer object key, then delete the previous.

 var data = {ABC:{key:'dynamic_key',value:'dynamic_value'}}
 
 data.CCC = {...data.ABC}
 delete data.ABC
 
 console.log(data)


That also could be (without using delete)

var data = {ABC:{key:'dynamic_key',value:'dynamic_value'}}

data = {CCC: data.ABC}
console.log(data)


EDIT... It you want to use a string from a variable to set the new key, use the bracket notation:

var data = {ABC:{key:'dynamic_key',value:'dynamic_value'}}
var newKey="CAR"

data[newKey] = data.ABC
delete data.ABC
console.log(data)

about 4 years ago · Juan Pablo Isaza Report

0

data.CCC = data.ABC // on object create new key name. Assign old value to this

delete data.ABC //delete object with old key name

output:

{ 
  "CCC":
    {
       "key": "dynamic_key",
       "value": "dynamic_value"
    }
}
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!