Created
August 14, 2016 18:44
-
-
Save Tahseenm/f0da828cc922b1b4618a65d4411de404 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