You will learn how to convert a map to an object in JavaScript. Some of the ways will be discussed in the article, such as using the Object. assign() or Object.fromEntries() method. Let’s go into detail now.
Convert a map to an object in JavaScript
Using the Object. assign() method
Copying the values of all enumerable properties from one source object to another from one or more other objects is accomplished with the help of Object.assign()
. The result will be this Object.
Syntax:
Object.assign(target, ...sources)
Parameters:
- target: Target object
- sources: Source objects
Because map and Object are two different value types, we have to use methods in Javascript to convert the map to object. As shown below, we will convert the map using Object. assign().
Code:
var map = new Map(); map.set("Tom", 20); map.set("Fin", 22); map.set("Phineas", 19); // Convert a map to an object using the Object.assign() method var userList = Array.from(map).reduce( (userList, [key, value]) => Object.assign(userList, { [key]: value }), {} ); console.log(userList);
Output:
{Tom: 20, Fin: 22, Phineas: 19}
We define the map’s key-value format and assign these key-value pairs to the new Object using Javascript’s built-in methods. We can convert a map of three users into a similar object and print it to the console. If you want, you can also refer to how to do it below.
Using Object.fromEntries() method
Object.fromEntries()
converts a value type into an object. You need to pass in a value with a format equivalent to the key – value Responding to the key-value format, we have 2 data types that can be met:
- Nested Array with key-value pairs
- Map objects
In this article, we will use it with a map. We can easily convert it to an object if you pass it the appropriate key-value.
Code:
var map = new Map(); map.set("Tom", 20); map.set("Fin", 22); map.set("Phineas", 19); // Convert a map to an object using the Object.fromEntries() method var userList = Object.fromEntries(map); console.log(userList);
Output:
{Tom: 20, Fin: 22, Phineas: 19}
With this approach, you only need to pass the map to be converted into the parameter. This function will do the rest for you. The method will find the key-value pairs inside the map, convert them to a “userList” object, and print them to the console. This method also gives the same results as the first method, good luck with the methods mentioned in the article.
Summary
To summarize, you already know how to convert a map to an object in JavaScript. However, it would help if you used the Object.fromEntries() way because this method is more concise and built to do this. Good lucks!
Maybe you are interested:
- Convert an Object to a Query String using JavaScript
- Convert an Object to a Map using JavaScript
- Convert an Object to JSON String using JavaScript

Hi, my name’s Nathaniel Kirk. I’m interested in learning and sharing knowledge about programming languages. My strengths are C, C++, Python, HTML, CSS, Javascript, and Reactjs. If you have difficulty with them, don’t worry. I’m here to support you.
Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Python, HTML, CSS, Javascript, Reactjs