Skip to content

Instantly share code, notes, and snippets.

@hperantunes
Created January 30, 2020 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hperantunes/962d526de3378ef23e3b5b17531a03e0 to your computer and use it in GitHub Desktop.
Save hperantunes/962d526de3378ef23e3b5b17531a03e0 to your computer and use it in GitHub Desktop.
const a = {
CoolButton: {
fr: "Omelette du fromage",
en: "Black pudding"
},
NotSoCoolButton: {
fr: "Une baguette",
en: "Apple pie"
}
};
const b = {
CoolButton: {
"pt-br": "Feijoada"
},
NotSoCoolButton: {
"fr": "Je suis un valeur modifié",
"pt-br": "Frango frito"
}
};
function isObject(item) {
return item && typeof item === "object" && !Array.isArray(item);
}
function mergeDeep(target, source) {
let output = Object.assign({}, target);
if (isObject(target) && isObject(source)) {
Object.keys(source).forEach(key => {
if (isObject(source[key])) {
if (!(key in target)) {
Object.assign(output, { [key]: source[key] });
} else {
output[key] = mergeDeep(target[key], source[key]);
}
} else {
Object.assign(output, { [key]: source[key] });
}
});
}
return output;
}
console.log(mergeDeep(a, b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment