0

How can I update an object with another object?

for example

master = { name: "John", email: "j@j.com" }

child = { phone: "888-333-3999" }

How can I update master with child?

Output of master need to be

{ name: "John", email: "j@j.com", phone: "888-333-3999" }

Also, this is a node.js JavaScript, I don't want to use jQuery.

Ajay
  • 6,418
  • 18
  • 79
  • 130
user3658423
  • 1,904
  • 5
  • 29
  • 49

1 Answers1

1

It's quite simple, you just need to iterate over one object and copy its properties/values into the other object:

for (key in child) master[key] = child[key];
user229044
  • 232,980
  • 40
  • 330
  • 338