Convert a Map to an Object in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
2 min

banner

# Convert a Map to an Object in JavaScript

To convert a Map to an object, call the Object.fromEntries() method passing it the Map as a parameter.

The Object.fromEntries method takes an iterable, such as a Map, and returns an object containing the key-value pairs of the iterable.

index.js
const map = new Map([ ['name', 'Bobby'], ['country', 'Chile'], ]); // โœ… Convert Map to Object const obj = Object.fromEntries(map); console.log(obj); // ๐Ÿ‘‰๏ธ {name: 'Bobby', country: 'Chile'} // โœ… Convert Object to Map const newMap = new Map(Object.entries(obj)); console.log(newMap); // ๐Ÿ‘‰๏ธ {'name' => 'Bobby', 'country' => 'Chile'}

convert map to object

The code for this article is available on GitHub

The Object.fromEntries() method takes an array of key-value pairs or a Map object and converts the value to an object

Here's an example of using the Object.fromEntries() method with an array of key-value pairs instead.

index.js
const obj = Object.fromEntries([ ['name', 'Bobby'], ['country', 'Chile'], ]); console.log(obj); // ๐Ÿ‘‰๏ธ {name: 'Bobby', country: 'Chile'}

The Object.fromEntries() method takes a two-dimensional array of key-value pairs and converts it to an object in the same way it handles a Map.

# Convert a Map to an Object using Map.forEach()

This is a three-step process:

  1. Declare a new variable and initialize it to an empty object.
  2. Use the Map.forEach() method to iterate over the Map.
  3. Assign each key-value pair to the new object.
index.js
const map = new Map([ ['name', 'Bobby'], ['country', 'Chile'], ]); const obj = {}; map.forEach((value, key) => { obj[key] = value; }); // ๐Ÿ‘‡๏ธ { name: 'Bobby', country: 'Chile' } console.log(obj);

convert map to object using map foreach

The code for this article is available on GitHub

The function we passed to the Map.forEach() method gets called for each key-value pair of the Map and gets passed the following parameters:

  1. value - the value of the iteration
  2. key - the key of the iteration
  3. map - the Map object that's being iterated

On each iteration, we assign the current key-value pair to the new object.

# Convert a Map to an Object using for...of

This is a three-step process:

  1. Declare a new variable and initialize it to an empty object.
  2. Use the for...of loop to iterate over the Map.
  3. Assign each key-value pair to the new object.
index.js
const map = new Map([ ['name', 'Bobby'], ['country', 'Chile'], ]); const obj = {}; for (const [key, value] of map) { obj[key] = value; } // ๐Ÿ‘‡๏ธ { name: 'Bobby', country: 'Chile' } console.log(obj);

convert map to object using for of

The code for this article is available on GitHub

The for...of statement is used to loop over iterable objects like arrays, strings, Map, Set and NodeList objects and generators.

On each iteration, we assign the current key-value pair to the new object.

After the last iteration, all key-value pairs of the Map are stored in the object.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev