Created
April 30, 2019 03:31
-
-
Save umran/ef2b8e08d2be7eb444722b8d4219e2b7 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 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a little snippet to flatten json objects in a node js environment in case someone else might find this useful.