Skip to content

Instantly share code, notes, and snippets.

@umran
Created April 30, 2019 03:31
Show Gist options
  • Save umran/ef2b8e08d2be7eb444722b8d4219e2b7 to your computer and use it in GitHub Desktop.
Save umran/ef2b8e08d2be7eb444722b8d4219e2b7 to your computer and use it in GitHub Desktop.
const util = require('util')
const flatten = (obj, progress, path=null) => {
Object.keys(obj).forEach(key => {
let parentPath = path ? path.concat('.', key) : key
if (isPrimary(obj[key])) {
progress[parentPath] = obj[key]
} else {
flatten(obj[key], progress, parentPath)
}
})
}
const isPrimary = obj => {
if (typeof obj === 'object') {
return util.isArray(obj) ||
util.isDate(obj)
}
return true
}
module.exports = obj => {
let converted = {}
flatten(obj, converted)
return converted
}
@umran
Copy link
Author

umran commented Apr 30, 2019

Here's a little snippet to flatten json objects in a node js environment in case someone else might find this useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment