Map conversion
Transform a Map into a plain object for APIs or serialization.
JavaScript · Function
Object.fromEntries() creates a new object from an iterable of key-value pairs. It accepts sources such as arrays and Maps, including entries with symbol keys.
Object.fromEntries() consumes an iterable that yields two-item entries. It creates a new ordinary object, using each entry's first item as a property key and its second item as that property's value; symbol keys are supported as well as string keys.
Object.fromEntries(iterable)| Name | Type | Description |
|---|---|---|
iterable | Iterable | An iterable, such as an Array or Map, whose iterator produces two-element array-like entries. The first item is a string or symbol key, and the second is its value. |
A new object whose properties are given by the entries of the iterable.
const entries = new Map([
["language", "JavaScript"],
["version", 2024],
]);
const result = Object.fromEntries(entries);
console.log(result); // { language: "JavaScript", version: 2024 }Converts a Map's key-value pairs into an ordinary object.
Transform a Map into a plain object for APIs or serialization.
Build an object from filtered or mapped key-value pairs.
Construct objects while retaining symbol-keyed properties.
It accepts an iterable whose iterator produces two-element array-like values. The first element becomes the property key, and the second becomes its value; the result is a new object.
Follow-up: What happens with a Map? A Map is iterable over its key-value pairs, so it can be passed directly without first converting it to an array.
The input must implement [Symbol.iterator](), and each produced entry must provide a key in position 0 and a value in position 1. Arrays and Maps are typical inputs.
Follow-up: Why can a custom iterable be used? Because the method depends on the iterable protocol rather than on a specific collection type.
It is the reverse of Object.entries() in the usual string-keyed case: entries turns an object's own enumerable string properties into pairs, while fromEntries builds an object from pairs. fromEntries additionally supports symbol keys.
Follow-up: Does it preserve the source object's prototype? No. The method creates a new ordinary object rather than cloning the source object or using a custom constructor.
Object.entries()Object.keys()Object.values()Map