creation
Object.fromEntries
Transforms a list of key-value pairs into an object — the inverse of Object.entries. Introduced in ES2019 and accepts any iterable of [key, value] pairs, including Map.
Object.fromEntries(iterable) Parameters
| Parameter | Purpose |
|---|---|
| iterable | Any iterable yielding [key, value] pairs (e.g. Map, array of pairs) |
| returns | A new object composed of the given entries |
Examples
console.log(Object.fromEntries([['a', 1], ['b', 2]])); Logs { a: 1, b: 2 }
const map = new Map([['x', 10], ['y', 20]]);
console.log(Object.fromEntries(map)); Logs { x: 10, y: 20 }
const params = new URLSearchParams('a=1&b=2');
console.log(Object.fromEntries(params)); Logs { a: '1', b: '2' }
const doubled = Object.fromEntries(
Object.entries({ a: 1, b: 2 }).map(([k, v]) => [k, v * 2])
);
console.log(doubled); Logs { a: 2, b: 4 } — the classic map-object transform pattern
Gotcha
Later entries with the same key overwrite earlier ones. Keys are coerced to strings unless they are Symbols; number keys become string keys.
Related methods
Object.entries
Returns an array of a given object's own enumerable string-keyed [key, value] pairs. Useful for iterating objects with for...of destructuring or converting to a Map.
Object.keys
Returns an array of a given object's own enumerable string-keyed property names. The order matches a normal for...in loop but excludes inherited and non-enumerable properties.
Object.assign
Copies all enumerable own properties from one or more source objects to a target object, then returns the modified target. Performs a shallow merge, invoking source getters and target setters.
Object.create
Creates a new object with the specified prototype and optional property descriptors. Pass null to create an object with no prototype at all.
Object.defineProperty
Defines a new property directly on an object, or modifies an existing one, with fine-grained control over its descriptor. Returns the object.