Created
October 20, 2021 18:53
-
-
Save KEIII/888e1de84fc4f4413065780ee5156239 to your computer and use it in GitHub Desktop.
Flatten a deep object into a one level object with it’s dot separated path as key
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
const flatten = object => { | |
const result = {}; | |
const go = (curr, parents) => { | |
Object.entries(curr).forEach(([key, value]) => { | |
const path = [...parents, key]; | |
if (value !== null && typeof value === 'object') { | |
go(value, path); | |
} else { | |
result[path.join('.')] = value; | |
} | |
}); | |
}; | |
go(object, []); | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment