Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jeffchiucp/06ea5dfb7ae09084aa730bcef8af753f to your computer and use it in GitHub Desktop.
Save jeffchiucp/06ea5dfb7ae09084aa730bcef8af753f 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