Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wolfiex/6af982c859dd2a83d6300cd005e6c6ba to your computer and use it in GitHub Desktop.
Save wolfiex/6af982c859dd2a83d6300cd005e6c6ba to your computer and use it in GitHub Desktop.
Convert ES6 Map to Object Literal
let map = new Map();
map.set("a", 1);
map.set("b", 2);
map.set("c", 3);
let obj = Array.from(map).reduce((obj, [key, value]) => (
Object.assign(obj, { [key]: value }) // Be careful! Maps can have non-String keys; object literals can't.
), {});
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