Skip to content

Instantly share code, notes, and snippets.

@mgk
Forked from davemackintosh/map-to-json.js
Created September 9, 2017 19:41
Show Gist options
  • Save mgk/86a182f7e97e909510589e18667b3cd1 to your computer and use it in GitHub Desktop.
Save mgk/86a182f7e97e909510589e18667b3cd1 to your computer and use it in GitHub Desktop.
Convert ES6 `Map`s to a standard JSON object without effing Babel.
/**
* Convert a `Map` to a standard
* JS object recursively.
*
* @param {Map} map to convert.
* @returns {Object} converted object.
*/
function map_to_object(map) {
const out = Object.create(null)
map.forEach((value, key) => {
if (value instanceof Map) {
out[key] = map_to_object(value)
}
else {
out[key] = value
}
})
return out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment