0
0
JavascriptHow-ToBeginner · 2 min read

JavaScript How to Convert Map to Object Easily

Use Object.fromEntries(yourMap) to convert a Map to an Object in JavaScript, where yourMap is the Map you want to convert.
📋

Examples

Inputnew Map([['a', 1], ['b', 2]])
Output{"a":1,"b":2}
Inputnew Map([[1, 'one'], [2, 'two']])
Output{"1":"one","2":"two"}
Inputnew Map()
Output{}
🧠

How to Think About It

To convert a Map to an Object, think of the Map as a list of key-value pairs. You want to take each pair and make it a property in a new Object. JavaScript provides a built-in method to do this easily by turning the Map entries into an Object's properties.
📐

Algorithm

1
Get the Map you want to convert.
2
Use Object.fromEntries() and pass the Map to it.
3
Return the new Object created from the Map entries.
💻

Code

javascript
const myMap = new Map([['name', 'Alice'], ['age', 25]]);
const obj = Object.fromEntries(myMap);
console.log(obj);
Output
{"name":"Alice","age":25}
🔍

Dry Run

Let's trace converting Map([['name', 'Alice'], ['age', 25]]) to an Object.

1

Start with Map

Map entries: [['name', 'Alice'], ['age', 25]]

2

Convert with Object.fromEntries

Object.fromEntries converts each pair to a property

3

Resulting Object

{"name":"Alice", "age":25}

Map KeyMap ValueObject PropertyObject Value
nameAlicenameAlice
age25age25
💡

Why This Works

Step 1: Map stores key-value pairs

A Map holds pairs like ['key', 'value'] which can be converted to object properties.

Step 2: Object.fromEntries converts pairs

The Object.fromEntries() method takes these pairs and creates an object with keys and values.

Step 3: Result is a plain object

The output is a normal JavaScript object with properties matching the Map's keys and values.

🔄

Alternative Approaches

Using a for-of loop
javascript
const myMap = new Map([['x', 10], ['y', 20]]);
const obj = {};
for (const [key, value] of myMap) {
  obj[key] = value;
}
console.log(obj);
More manual but works in older environments without Object.fromEntries.
Using Array.reduce
javascript
const myMap = new Map([['a', 1], ['b', 2]]);
const obj = [...myMap].reduce((acc, [key, value]) => {
  acc[key] = value;
  return acc;
}, {});
console.log(obj);
Uses array methods to build the object, useful if you want to transform keys or values.

Complexity: O(n) time, O(n) space

Time Complexity

The conversion loops through all Map entries once, so it takes linear time relative to the number of entries.

Space Complexity

A new object is created with the same number of properties as the Map entries, so space grows linearly.

Which Approach is Fastest?

Using Object.fromEntries() is generally fastest and most readable; manual loops or reduce add more code but similar performance.

ApproachTimeSpaceBest For
Object.fromEntriesO(n)O(n)Simple and modern code
For-of loopO(n)O(n)Older environments without Object.fromEntries
Array.reduceO(n)O(n)When transforming keys or values during conversion
💡
Use Object.fromEntries() for a clean and simple conversion from Map to Object.
⚠️
Trying to convert a Map to Object by directly assigning it without using Object.fromEntries() or iteration.