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

212
Visualizações
'Moving' properties from an object into a new object, based on a 3rd object

I have an object which contains multiple properties. For a given list of props in a 3rd object, I want to 'move' the properties from the original object and put them into a new object.

I have a 'longhand' way of doing this at the moment, something like the following. However this creates 2 new objects, rather than just 'moving' some of the keys out:

let pets = {
  'cat': {},
  'dog': {},
}

let animals = {
  'cat': 'black',
  'dog': 'white',
  'whale': 'blue',
  'albatross': 'grey',
}

let aPet = {}
let notAPet = {}
for (let [key, val] of Object.entries(animals)) {
  if (key in pets) {
    aPet[key] = val
  } else {
    notAPet[key] = val
  }
}

Is there a nicer 'short' way of achieving the above, preferably only creating aPet while making animals end up equivalent to notAPet?

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

0

You can use delete operator:

let pets = {
  'cat': {},
  'dog': {},
}

let animals = {
  'cat': 'black',
  'dog': 'white',
  'whale': 'blue',
  'albatross': 'grey',
}

let aPet = {}
let notAPet = {}
for (let [key, val] of Object.entries(animals)) {
  if (key in pets) {
    aPet[key] = val
    delete animals[key]
  }
}

console.log(aPet, animals)

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