Created
November 24, 2017 13:03
-
-
Save titogeorge/9549cca777a67ec0c84a25e65be9e616 to your computer and use it in GitHub Desktop.
Flatten and given java script object.
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
function flatten(originalObject, newObj, keyNamePrefix) { | |
if (newObj == undefined) { | |
newObj = {}; | |
} | |
if (typeof originalObject == 'object') { | |
for (key in originalObject) { | |
if (typeof originalObject[key] == 'object') { | |
let item = originalObject[key]; | |
if (keyNamePrefix == undefined) { | |
flatten(item, newObj, key); | |
} else { | |
flatten(item, newObj, keyNamePrefix + "."+ key); | |
} | |
} else { | |
if (keyNamePrefix == undefined) { | |
newObj[key] = originalObject[key]; | |
} else { | |
newObj[keyNamePrefix + '.' + key] = originalObject[key]; | |
} | |
} | |
} | |
} | |
return newObj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Convert
to