JSFiddle - React, Tailwind, and code Playground
JavaScript
function merge(obj1,obj2){
var result = {};
for(i in obj1){
result[i] = obj1[i];
if((i in obj2) && (typeof obj1[i] === "object") && (i !== null)){
result[i] = merge(obj1[i],obj2[i]);
}
}
for(i in obj2){
if(i in result){ //conflict
continue;
}
result[i] = obj2[i];
}
return result;
}
var json1 = {
"key": "abc123def456",
"message": {
"subject": "Has a Question",
"from": "example1@example.com",
"to": "example2@example.com"
}
};
var json2 = {
"key": "00700916391", //Overwrite me
"message": { //Dont overwrite me!
"subject": "Hey what's up?", //Overwrite me
"something": "something new" //Add me
}
};
console.log(merge(json1, json2));