Convert object to Map

JavaScript, Object · Jun 16, 2022

Converts an object to a Map.

  • Use Object.entries() to convert the object to an array of key-value pairs.
  • Use the Map constructor to convert the array to a Map.
const objectToMap = obj => new Map(Object.entries(obj));
objectToMap({a: 1, b: 2}); // Map {'a' => 1, 'b' => 2}

Written by Angelos Chalaris

I'm Angelos Chalaris, a JavaScript software engineer, based in Athens, Greece. The best snippets from my coding adventures are published here to help others learn to code.

If you want to keep in touch, follow me on GitHub.

More like this