Created
January 30, 2020 16:42
-
-
Save hperantunes/962d526de3378ef23e3b5b17531a03e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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