Skip to content

Instantly share code, notes, and snippets.

@greglecki
Last active July 1, 2020 14:49
Show Gist options
  • Select an option

  • Save greglecki/d55e8ec79b71021ee4c34466b7d764aa to your computer and use it in GitHub Desktop.

Select an option

Save greglecki/d55e8ec79b71021ee4c34466b7d764aa to your computer and use it in GitHub Desktop.
Get diff of two js objects
function getDiff(baseTranslation, targetTranslation) {
var result = {};
Object.keys(baseTranslation).forEach((key) => {
var obj = targetTranslation[key];
if (!obj || (typeof(obj) === 'string' && JSON.stringify(obj) === JSON.stringify(baseTranslation[key]))) {
result[key] = baseTranslation[key];
} else if (typeof(obj) === 'object') {
const o = getDiff(baseTranslation[key], obj);
if (Object.keys(o).length > 0) {
result[key] = o;
}
}
});
return result;
}
// Then right click on the object -> Store as global variable
// This should give you an variable name like temp1
// Type in console copy(type1) to copy to the clipboard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment