This code merges two objects. How to amend it so that a property is discarded of the first object if it exists in the second object?
E.g. object 1 has this property:
surname: {
test: '123',
other: '124'
},
and object 2:
surname: {
test: '124'
},
The merge would use the property of object 2 (other: '124'
would not exist in the merged result)
function merge(...objects) {
function m(t, s) {
Object.entries(s).forEach(([k, v]) => {
t[k] = v && typeof v === 'object' ? m(t[k] || {}, v) : v;
});
return t;
}
return objects.reduce(m, {});
}
var obj1 = {
name: '112',
surname: {
test: '123',
other: '124'
},
age: 151,
height: '183',
weight: 80
},
obj2 = {
name: '114',
surname: {
test: '124'
},
age: 151,
height: 184,
weight: 81
},
result = merge(obj1, obj2);
console.log(result);