JavaScript How to Convert Map to Object Easily
Object.fromEntries(yourMap) to convert a Map to an Object in JavaScript, where yourMap is the Map you want to convert.Examples
How to Think About It
Algorithm
Code
const myMap = new Map([['name', 'Alice'], ['age', 25]]); const obj = Object.fromEntries(myMap); console.log(obj);
Dry Run
Let's trace converting Map([['name', 'Alice'], ['age', 25]]) to an Object.
Start with Map
Map entries: [['name', 'Alice'], ['age', 25]]
Convert with Object.fromEntries
Object.fromEntries converts each pair to a property
Resulting Object
{"name":"Alice", "age":25}
| Map Key | Map Value | Object Property | Object Value |
|---|---|---|---|
| name | Alice | name | Alice |
| age | 25 | age | 25 |
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
const myMap = new Map([['x', 10], ['y', 20]]); const obj = {}; for (const [key, value] of myMap) { obj[key] = value; } console.log(obj);
const myMap = new Map([['a', 1], ['b', 2]]); const obj = [...myMap].reduce((acc, [key, value]) => { acc[key] = value; return acc; }, {}); console.log(obj);
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Object.fromEntries | O(n) | O(n) | Simple and modern code |
| For-of loop | O(n) | O(n) | Older environments without Object.fromEntries |
| Array.reduce | O(n) | O(n) | When transforming keys or values during conversion |
Object.fromEntries() for a clean and simple conversion from Map to Object.Object.fromEntries() or iteration.