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

105
Views
Restructure an array of objects in JS so key and value pairs match

I have an array with objects like this

[ 
  {name: 'Donegal', code: 'DL'}, 
  {name: 'Dublin', code: 'DUB'}, 
  {name: 'Meath', code: 'MH'}
]

How do I restructure it so it looks like this

[ 
  {Donegal: 'Donegal'}, 
  {Dublin: 'Dublin'}, 
  {Meath: 'Meath'}
]

**** EDIT ****

Apologies, but after receiving feedback, I looked at my question again and realized that I wrote the desired object incorrectly, apologies for that. Regardless, the question has been answered (Thank you everyone for your comments and answers). For the record, here is the desired output

[ 
  {
    Donegal: 'Donegal', 
    Dublin: 'Dublin', 
    Meath: 'Meath'
  }
]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The structure you are targeting looks wrong: having an array with little objects that have one dynamic property, kills any benefit you would have from using object keys.

Instead go for one object (not an array):

let input = [ 
  {name: 'Donegal', code: 'DL'}, 
  {name: 'Dublin', code: 'DUB'}, 
  {name: 'Meath', code: 'MH'}
];

let output = Object.fromEntries(
    input.map(({name}) => [name, name])
);

console.log(output);

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!