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

179
Visualizações
How to "migrate" one object to another object in JavaScript?

Given two object, named: ObjectA and ObjectB:

var ObjectA = {
    config1: "5",
    config2: "6",
    config3: "7"
}
var ObjectB = {
    config1: "1",
    config2: "2",
    config4: "3",
    config5: "4"
}

How can I copy from ObjectB to ObjectA so that ObjectA have the same properties of ObjectB while the values of properties in ObjectA is kept and values from ObjectB that doesn't exist in ObjectA is copied to ObjectA?

Expected result:

ObjectA = {
    config1: "5",
    config2: "6",
    config4: "3",
    config5: "4"
}
  • config3 is deleted since it doesn't exist in ObjectB,
  • config4 with config5 are copied to ObjectA since those doesn't exist in ObjectA,
  • and value in config1 with config2 are kept the same in ObjectA since those two exist in both of the object)
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Use reduce:

ObjectA = Object.keys(ObjectB).reduce(
    (acc, key) => ({ ...acc, [key]: ObjectA[key] ?? ObjectB[key]}), {}
)

This will use the keys from ObjectB, take values from ObjectA, when they exist (with fallback to ObjectB), and populate an empty object with the results.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use the reducer function to solve.

var ObjectA = {
    config1: "5",
    config2: "6",
    config3: "7"
}
var ObjectB = {
    config1: "1",
    config2: "2",
    config4: "3",
    config5: "4"
}

const res = Object.keys(ObjectB).reduce((prev, next) => {
  if(ObjectA[next]){
    prev[next] = ObjectA[next];
  }
  return prev;
}, {...ObjectB})
console.log(res)

about 4 years ago · Juan Pablo Isaza Relatório

0

Here's a possible solution that is more old school: What this basically do:

  • Loop over the properties in ObjectA. If it doesn't exist in ObjectB, delete
  • Loop over the properties in ObjectB. If it doesn't exist in ObjectA, copy it.
function mergeObject(oldObj: Object, newObj: Object) {
    var copyOfOldObj = Object.assign({}, oldObj);
    for (const key in copyOfOldObj) {
        if (!newObj.hasOwnProperty(key)) {
            delete copyOfOldObj[key];
        }
    }
    for (const key in newObj) {
        if (!copyOfOldObj.hasOwnProperty(key)) {
            copyOfOldObj[key] = newObj[key];
        }
    }
    return copyOfOldObj;
}
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