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

166
Views
merging objects delete initial keys

I have two objects obj1 & obj2.

when I merge with:

const obj1 = {
  error_to_retrieve: "teste",
  error_to_search_customer: "",
  error_to_sync_timezone: "",
  error_to_update_avatar: "",
  error_to_update_profile: "",
  error_to_validate_customer_token: "",
  expired_session: "",
  invalid_token: "",
  invalid_token_text: ""
}

const obj2 = {
  error_to_retrieve: "test",
  error_to_search_customer: "test",
  error_to_sync_timezone: "test",
  error_to_update_avatar: "test",
  error_to_validate_customer_token: "test",
  expired_session: "test",
  invalid_token: "test",
  invalid_token_text: "test"
}


const obj3 = {...obj1, ...obj2};

console.log(obj3);

I get the full obj2, and it dont have the key error_to_update_profile.

How I maintain the key of the first object that my second dont have?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Use Object.prototype.hasOwnProperty() to check whether your final objects has the property key error_to_update_profile. The funtion returns true which is prove that the merge worked perfectly fine as intended.

const obj1 = {
  error_to_retrieve: "teste",
  error_to_search_customer: "",
  error_to_sync_timezone: "",
  error_to_update_avatar: "",
  error_to_update_profile: "",
  error_to_validate_customer_token: "",
  expired_session: "",
  invalid_token: "",
  invalid_token_text: ""
};

const obj2 = {
  error_to_retrieve: "test",
  error_to_search_customer: "test",
  error_to_sync_timezone: "test",
  error_to_update_avatar: "test",
  error_to_validate_customer_token: "test",
  expired_session: "test",
  invalid_token: "test",
  invalid_token_text: "test"
};

const obj3 = { ...obj1, ...obj2 };

console.log(obj3.hasOwnProperty("error_to_update_profile"));

about 4 years ago · Juan Pablo Isaza Report

0

You can change the order of what to write, i.e. whatever you put last will overwrite all previous entries { ...obj2, ...obj1 } → obj1 will overwrite obj2 if there are duplicate keys

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!