Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!