2

I have two JavaScript objects like

let master = {A:0, B:2};
let slave = {B:1, C:1};

I need:

result == {A:0, B:2, C:1};

Does JS have a simple command to merge in that way?

Stepan Loginov
  • 1,667
  • 4
  • 22
  • 49

1 Answers1

4

Use Object.assign

The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.

let master = {A:0, B:2};
let slave = {B:1, C:1};

console.log(Object.assign(slave, master));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392