Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

106
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda