Skip to content

Instantly share code, notes, and snippets.

@Tahseenm
Created August 14, 2016 18:44
Show Gist options
  • Save Tahseenm/f0da828cc922b1b4618a65d4411de404 to your computer and use it in GitHub Desktop.
Save Tahseenm/f0da828cc922b1b4618a65d4411de404 to your computer and use it in GitHub Desktop.
// Make new flatDict and return it
function flattenDictionary(dict) {
const go = (dict, initialKey, flatDict) => {
for (const key of Object.keys(dict)) {
const value = dict[key];
const flatKey = initialKey ? `${initialKey}.${key}` : key;
// When the value is a primitive
if (typeof value !== 'object') flatDict[flatKey] = value;
// When value is a object, flatten it
else go(value, flatKey, flatDict);
}
return flatDict;
}
return go(dict, null, {});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment