Skip to content

Instantly share code, notes, and snippets.

@paurakhsharma
Last active June 18, 2020 03:15
Show Gist options
  • Save paurakhsharma/4455f3e5dced24d0d2fe03784702cfd5 to your computer and use it in GitHub Desktop.
Save paurakhsharma/4455f3e5dced24d0d2fe03784702cfd5 to your computer and use it in GitHub Desktop.
Find the difference in a keys of two different objects
/**
* This script compares two different objects
* and finds a key value pair for keys that are missing in one object
* and keys that are extra in another
*/
let english_dict = {
home: {
button: "New button",
menu: "About"
},
about: "This is about page",
contact: "This is contact page"
}
let translated_dict = {
home: {
button: "नयाँ बटन",
checkbox: "चेकबक्स"
},
about: "यो पृष्ठ को बारे मा छ",
blog: "यो ब्लग पृष्ठ हो"
}
function findKeys(obj) {
var result = {};
function recursivelyFindKeys(o, prevKey) {
Object.keys(o).forEach(function (key) {
let id = prevKey === '' ? key : prevKey + '.' + key
if (typeof o[key] === 'object') {
recursivelyFindKeys(o[key], key);
} else {
result[id] = o[key];
}
});
}
recursivelyFindKeys(obj, '');
return result;
}
let english_key_values = findKeys(english_dict)
let translated_key_values = findKeys(translated_dict)
let english_keys = Object.keys(english_key_values)
let translated_keys = Object.keys(translated_key_values)
let english_keys_set = new Set(english_keys)
let translated_keys_set = new Set(translated_keys)
let missing = new Set(
[...english_keys_set].filter(x => !translated_keys_set.has(x)));
let extra = new Set(
[...translated_keys_set].filter(x => !english_keys_set.has(x)));
let missing_strings = [...missing].map(element => {return {[element]: english_key_values[element]}})
let unnecessary_strings = [...extra]
console.log(missing_strings)
console.log(unnecessary_strings)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment