Skip to content

Instantly share code, notes, and snippets.

@KEIII
Created October 20, 2021 18:53
Show Gist options
  • Save KEIII/888e1de84fc4f4413065780ee5156239 to your computer and use it in GitHub Desktop.
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
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