Stepan Loginov Stepan Loginov - 1 year ago 105
Javascript Question

merge two json with priority

I have two json like

let masterJson = {A:0, B:2};
let slaveJson = {B:1, C:1};

I need:

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

Did JS have simple command to merge in that way?

Answer Source

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 masterJson = {A:0, B:2};
let slaveJson = {B:1, C:1};

console.log(Object.assign(slaveJson, masterJson));

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download