-
-
Save mgk/86a182f7e97e909510589e18667b3cd1 to your computer and use it in GitHub Desktop.
Convert ES6 `Map`s to a standard JSON object without effing Babel.
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
/** | |
* 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