Forked from lukehorvat/es6-map-to-object-literal.js
Last active
May 11, 2022 00:49
-
-
Save AndrewJHart/0026cf153f91ae25a9c20a024267f62b to your computer and use it in GitHub Desktop.
Convert ES6 Map to Object Literal
This file contains hidden or 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
let map = new Map(); | |
map.set("a", 1); | |
map.set("b", 2); | |
map.set("c", 3); | |
let obj = [...map.entries()] // or Array.from(map) - either works; | |
.reduce((acc, [key, value]) => | |
({ ...acc, [key]: value }), // Can also spread in reduce or assign, latter is quicker but I love spread syntax lol | |
{} | |
); | |
console.log(obj); // => { a: 1, b: 2, c: 3 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment