Created
November 15, 2017 22:10
-
-
Save corysimmons/89773c11971d05db747b32db58f32e46 to your computer and use it in GitHub Desktop.
Convert an object to an array with the object keys chained with dots. So `{ a: 1, b: { c: 2 } }` comes back as `['a', 'b.c']`
This file contains hidden or 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 isNumeric = value => (value == Number(value)) ? 'number' : 'string' // eslint-disable-line | |
| let keys = [] | |
| let prependable = '' | |
| const flatMap = obj => { | |
| Object | |
| .keys(obj) | |
| .forEach(key => { | |
| if (typeof obj[key] !== 'object' && isNumeric(key) !== 'number') { | |
| keys.push(`${prependable}${key}`) | |
| return | |
| } | |
| if (typeof obj[key] === 'object') { | |
| prependable += `${key}.` | |
| flatMap(obj[key]) | |
| prependable = '' | |
| } | |
| }) | |
| keys = keys.sort() | |
| } | |
| flatMap(YOUR_OBJ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment