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

109
Visualizações
What's the most succinct syntax to preserve only specific keys from a large JavaScript object?

In other words, I want to clean objects by dropping most of their keys.

Say a third party API returns JSON with a large number of of attributes you don't care about.

obj = {
  name: ...,
  id: ...,
  description: ...,
  blah: ...,
  bloop: ...,
  blip: ...,
  ... 12 others
}

But you're only interested in, say, id and name.

I know there's spread syntax object destructuring that allows me to put these into separate variables.

const { id, name } = obj

Is there a way to transform obj into a new object that looks like the following without explicitly accessing every key and value and making an object out of those?

newObj = {
  id: ...,
  name: ...
}

I'm curious if there's a one-liner I can put into map to transform a whole array of these objects.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Just destructure the wanted properties and use short hand properties for the object.

result = array.map(({ id, name }) => ({ id, name }));
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