Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

merge two JavaScript objects with priority [duplicate]

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?

like image 552
Stepan Loginov Avatar asked Dec 01 '25 09:12

Stepan Loginov


1 Answers

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));
like image 85
Nina Scholz Avatar answered Dec 04 '25 08:12

Nina Scholz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!