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

154
Visualizações
Javascript overwrite part of a nested object with object merging

I'm working on a JS project where I need to override some values in my object which contains nested objects.

I'd normally use the following:

const merged = { ...application, ...customer }

So that any data inside of customer can override the application.

However, in my example, customer is overriding the entire applicant nested object and I just need to override the name within that object?

I've put together a JS fiddle which can be found here, what am I missing?

const customer = {
  applicant: {
    first_name: 'john'
  }
}

const application = {
  applicant: {
    first_name: 'edward',
    age: 50
  },
  income: {
    salary: 500
  }
}

const merged = { ...application, ...customer }

console.log(merged)

In merged I expect the first_name to be "John" whilst everything else remains in tact.

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

0

The properties you want to replace via spreading have to be at the top level of the object. In this case you take the top-level properties of application, which are applicant and income, and then replace the applicant property with that from customer. If you want the name to be replaced you would need something like

const merged = {
  ...application, 
  applicant: {...application.applicant, ...customer.applicant}
};
about 4 years ago · Juan Pablo Isaza Relatório

0

You can do this easily with lodash

const merged = _.merge(customer, application)

https://lodash.com/docs/4.17.15#merge

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