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

120
Views
Convert an array of objects to an object

I have an array of objects like this

const values = [{
    existing_value: 'Headline 56',
    new_value: 'Headline_new56',
  },
  {
    existing_value: 'Headline 59',
    new_value: 'Headline_new59',
  },
]

And I want to convert into this form.

const newObject = { "Headline 56": "Headline_new56",  "Headline 59": "Headline_new59"}

I was thinking something like this could work

const newObject = {}

const testvalues = values.map(obj => Object.assign( newObject, obj.existing_value: obj.new_value)) 

Syntactically it's not quite right. What is the best way to go about it?

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

Object.fromEntries is a possible approach

const values = [{
    existing_value: 'Headline 56',
    new_value: 'Headline_new56',
  },
  {
    existing_value: 'Headline 59',
    new_value: 'Headline_new59',
  },
]

const newObj = Object.fromEntries(values.map(({existing_value,new_value})=>[existing_value,new_value]))

console.log(newObj)

about 4 years ago · Santiago Gelvez Report

0

Use Array reduce

const values = [{
    existing_value: 'Headline 56',
    new_value: 'Headline_new56',
  },
  {
    existing_value: 'Headline 59',
    new_value: 'Headline_new59',
  },
];

const obj = values.reduce((a, b) => ({ ...a,
  [b.existing_value]: b.new_value
}), {})

console.log(obj)

about 4 years ago · Santiago Gelvez Report

0

Extracted from mozilla webdocs

const values = [{
        existing_value: 'Headline 56',
        new_value: 'Headline_new56',
    },
    {
        existing_value: 'Headline 59',
        new_value: 'Headline_new59',
    },
];

const newObj = values.map((obj) => {
    var rObj = {};
    rObj[obj.existing_value] = obj.new_value;
    return rObj;
});

console.log(newObj);

about 4 years ago · Santiago Gelvez 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!