Skip to content

Instantly share code, notes, and snippets.

@davemackintosh
Last active August 7, 2023 15:49
Show Gist options
  • Save davemackintosh/3b9c446e8681f7bbe7c5 to your computer and use it in GitHub Desktop.
Save davemackintosh/3b9c446e8681f7bbe7c5 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
}
@lukaabra
Copy link

Thanks!

@MatheusHonorato
Copy link

Thanks!!!

@Ojonathan
Copy link

Thanks !!!

@SkyWalker1005
Copy link

Thanks!!!

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