Skip to content

Instantly share code, notes, and snippets.

@corysimmons
Created November 15, 2017 22:10
Show Gist options
  • Save corysimmons/89773c11971d05db747b32db58f32e46 to your computer and use it in GitHub Desktop.
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']`
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