Object.fromEntries
- Version
- es2025
- Since
- es2019
- Review
- Draft
Summary#
Object.fromEntries builds an object from iterable key-value pairs. Pass it an array of pairs or a Map when you need properties on an ordinary object.
Syntax#
const object = Object.fromEntries(iterableOfPairs);
Example#
const preferences = new Map([
["theme", "dark"],
["density", "compact"],
]);
const settings = Object.fromEntries(preferences);
console.log(JSON.stringify(settings));
Expected output#
{"theme":"dark","density":"compact"}.
Common mistakes#
Repeated keys overwrite previous values. Keys become property keys, so distinct object keys in a Map can turn into the same string property. Keep a Map when you need the original key identity.
Version applicability#
ES2019 added Object.fromEntries. Use a polyfill on earlier runtimes. Assigning entries to {} in a loop can change its prototype if a key is "__proto__".
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0