Created
June 13, 2019 14:27
-
-
Save daitonaaa/d0db3a9d932af78e80dc4191ca89eabd 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
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