Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewJHart/0026cf153f91ae25a9c20a024267f62b to your computer and use it in GitHub Desktop.
Save AndrewJHart/0026cf153f91ae25a9c20a024267f62b 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 = [...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