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

182
Views
How we can replace the keys of an Object with new key?

I have an object like below

[
  {
   "day"  : "monday",
   "value": 1
   },
  {
   "day"  : "tuesday",
   "value": 2
   },
   ...
]

Is there any native javascript way to replace the key with a new key. Here I need to replace the "day" & "value" with "x" & "y" respectively. (like below)

[
  {
   "x"  : "monday",
   "y": 1
   },
  {
   "x"  : "tuesday",
   "y": 2
   },
   ...
]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

you can use map and inside the call back create a new object and modify it as required. Then return this object

const data = [{
    "day": "monday",
    "value": 1
  },
  {
    "day": "tuesday",
    "value": 2
  }
];

const newData = data.map((item) => {
  const obj = {};
  for (let keys in item) {
    if (keys === 'day') {
      obj['x'] = item[keys]
    }
    if (keys === 'value') {
      obj['y'] = item[keys]
    }


  };
  return obj
});
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!