Skip to content

Instantly share code, notes, and snippets.

@daitonaaa
Created June 13, 2019 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daitonaaa/d0db3a9d932af78e80dc4191ca89eabd to your computer and use it in GitHub Desktop.
Save daitonaaa/d0db3a9d932af78e80dc4191ca89eabd to your computer and use it in GitHub Desktop.
const testObj = {
b: {
c: 1,
d: 2,
e: {
aa: 'test',
}
},
a: {
be: 'abe',
ce: 'ace'
}
};
function objectEntriesToKeys(fromObject) {
const results = {};
const getKeyPath = (obj, thisKey = '') => {
for (let key in obj) {
if (thisKey) {
thisKey += `_${key}`;
} else {
thisKey = key;
}
if (typeof obj[key] === 'object') {
getKeyPath(obj[key], thisKey)
} else {
results[thisKey] = obj[key];
thisKey = '';
}
}
}
getKeyPath(fromObject);
return results;
};
objectEntriesToKeys(testObj);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment