Convert object to Map

Angelos Chalaris · 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}

Recommended snippets