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

90
Visualizações
how to deep copy a JavaScript object from another without adding new properties

Here I have a default object with nested properties for eg -

default = { 
key1: val1;
key2: {
  key3: val3,
  key4: val4
  }
}

and a new object with new values for same properties as above and some extra properties of its own

newObject = {
key1: val11,
key2: {
  key3: val31,
  key5: val51,
  }
}

Now I need to replace all the available nested properties of default with newObject without adding any new property to default. The skeleton of default should never change. Also this needs to be a deep copy. So the result here should look like:

result = { 
key1: val11;
key2: {
  key3: val31,
  key4: val4,
  }
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I added code for you but if you need more complex then you may want to use some js packages, but I think it should be sufficient.

Here you go:

let objectDefault = {
      key1: 'val1',
      key2: {
        key3: 'val3',
        key4: 'val4'
      }
    }
    
let otherObject = {
  key1: 'val5',
  key2: {
    key3: 'val6',
    key4: 'val7'
  },
  key10: 'test'
}

let filteredValuesOfOthedObject = Object.fromEntries(Object.entries(otherObject).filter(([key]) => Object.keys(objectDefault).includes(key)));

let newObject = {...objectDefault, ...filteredValuesOfOthedObject}

console.log(newObject)
about 4 years ago · Juan Pablo Isaza Relatório

0

Take a look at this one. // Parent Object let _default = { key1: 'val1', key2: { key3: 'val3', key4: 'val4', key6: { key7: 'test', key10: 'val10' } }, key11: 'val231' };

// Output object
let newObj = {
    key1: 'val11',
    key2: {
        key3: 'val31',
        key5: 'val51',
        key6: {
            key7: 'val31',
            key8: 'val43'
        },
        key12: 'val212'
    }
};

// Utils to check if value if object
function isObject(value) {
    return typeof (value) === 'object' && !(value instanceof Date) && !Array.isArray(value) && !Object.is(value, null) && !Object.is(value, undefined) && !(value instanceof Function)
}

// Recursive function to add 
function recursive(obj, ouputObj) {
    Object.keys(obj).map((el, i) => {
        let val = obj[el];
        if (!ouputObj?.[el]) ouputObj[el] = val;
        if (isObject(val)) recursive(val, ouputObj?.[el] || {});
    })
    return ouputObj;
}

newObj = recursive(_default, newObj);
console.log(`~~~~~~~~~~~~~~~, `, newObj);
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