-
-
Save jeffchiucp/06ea5dfb7ae09084aa730bcef8af753f 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
// 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