-1

I have two objects.

ObjectA = {name: 'Peter', age: 56, country: 'USA'}

ObjectB = {age: 34}

I want to update ObjectA to become

{name: 'Peter', age: 34, country: 'USA'}

if (ObjectB) {
    const newObject = ObjectA[Object.keys(ObjectB)[0]] === ....
}

What would be a better way to update ObjectA?

susu watari
  • 177
  • 1
  • 2
  • 11
  • 3
    Possible duplicate of [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) – jlewkovich Mar 04 '19 at 17:27
  • Possible duplicate of [What is destructuring assignment and its uses?](https://stackoverflow.com/questions/54605286/what-is-destructuring-assignment-and-its-uses) – Code Maniac Mar 04 '19 at 17:32
  • `{...objecta,...objectb}` will do the job – Code Maniac Mar 04 '19 at 17:34

2 Answers2

-1

Just use Object.assign(ObjectA, ObjectB)

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
-1

ES9/Regular Javascript

let object1 = {
 foo: 'foo'
}
let object2 = {
 bar: 'bar'
}

console.log({ ...object1, ...object2 })
console.log(Object.assign(object1, object2))
Adrien Rosi
  • 132
  • 5