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

180
Visualizações
quickest way to assign values of keys which are intersection in the target and source object

I want to assign only those keys which are already present in the target object and not the other keys that are done by the object.assign method.

example
a = { x: 2}
b = {x:3, y:4}

then result = {x:3}

I know there are other ways in past but is there any newer and better ways to do this like in one line?

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

0

You can reduce the keys of the target object to a new object by taking the values from the obj if they exist:

const fn = (target, obj) => Object.keys(target)
  .reduce((acc, key) => key in obj 
    ? { ...acc, [key]: obj[key] } 
    : acc
  , {})

const a = { x: 2}
const b = { x:3, y:4 }

const result = fn(a, b)

console.log(result)

about 4 years ago · Juan Pablo Isaza Relatório

0

I think the fastest way is:

for (let key in b) {
  if (a[key] === undefined) {
    delete b[key];
  }
}
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