0

I have an object like this

obj = {
a: 1
b: 1
c: 1
}

and another object like this

other = {
a: 2
b: 2
}

How can I update obj to become this

result = {
a: 2
b: 2
c: 1
}

without explicitly saying

obj.a = other.a
obj.b = other.b
etc...

since I have a lot of values to update.

Shrey Joshi
  • 1,066
  • 8
  • 25
  • 1
    You can spread both into a resulting object, `const result = {...obj, ...other}` (note that if `other` has properties `obj` doesn't have, then this will add those properties also to the result) – Nick Parsons Jul 21 '21 at 23:04
  • In my case, other will never have properties that obj doesn't have. can you make this into an answer so I can accept it? – Shrey Joshi Jul 21 '21 at 23:09
  • 1
    Duplicate: [How can I merge properties of two JavaScript objects dynamically?](https://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically) –  Jul 21 '21 at 23:09

0 Answers0