Object.fromEntries()
Object.fromEntries(iterable) Object.fromEntries() transforms an iterable of key-value pairs into an object. This method is the inverse of Object.entries() and provides a convenient way to convert Maps, Arrays, or any iterable yielding two-element arrays into a plain object.
Introduced in ES2019, fromEntries filled a gap in the language: entries() let you turn an object into an array of pairs, but converting back required a manual reduce. The two methods together make it natural to pipeline object transformations — filter, map, or sort the pairs as an array, then reconstruct the object with fromEntries.
Syntax
Object.fromEntries(iterable)
Parameters
| Parameter | Type | Description |
|---|---|---|
iterable | Array or Map | An iterable yielding key-value pairs like [[key, value], …] or a Map |
The iterable argument gives fromEntries() its flexibility — any object that implements the iterable protocol and yields two-element arrays is valid input. Arrays of pairs and Map objects are the most common, but URLSearchParams instances and custom iterables also work directly.
Examples
Converting an array of pairs
const pairs = [["name", "Alice"], ["age", 30], ["city", "NYC"]];
const obj = Object.fromEntries(pairs);
console.log(obj);
// { name: "Alice", age: 30, city: "NYC" }
An array of pairs is the simplest input shape, but any iterable that yields two-element arrays works the same way. This includes Map objects, which iterate their entries as [key, value] pairs natively — so passing a Map to fromEntries() converts it to a plain object in one call.
Converting a Map to an object
const map = new Map([
["framework", "React"],
["version", "18"],
["type", "library"]
]);
const obj = Object.fromEntries(map);
console.log(obj);
// { framework: "React", version: "18", type: "library" }
Map conversion is lossless for string keys — every entry in the Map becomes a property on the resulting object. This is convenient when you have accumulated data in a Map and need to pass it to an API or library that expects a plain object.
URL search params to object
const params = new URLSearchParams("name=John&age=25&city=Boston");
const obj = Object.fromEntries(params);
console.log(obj);
// { name: "John", age: "25", city: "Boston" }
URLSearchParams is iterable and yields [key, value] pairs, so it plugs directly into fromEntries() without any conversion step. All values come out as strings, though — if you need typed values, map through Object.entries() first and parse each field.
Transforming object values
const prices = { apple: 10, banana: 20, orange: 15 };
const doubled = Object.fromEntries(
Object.entries(prices).map(([key, value]) => [key, value * 2])
);
console.log(doubled);
// { apple: 20, banana: 40, orange: 30 }
The entries() → map() → fromEntries() pipeline is the standard way to transform an object’s values without mutating the original. Each pair flows through the mapper independently, so you can apply any computation — multiplication, formatting, type conversion — in a single pass.
Common Patterns
Filtering object properties
const user = { name: "Alice", age: 30, password: "secret123", token: "abc" };
// Remove sensitive fields
const safe = Object.fromEntries(
Object.entries(user).filter(([key]) => !["password", "token"].includes(key))
);
console.log(safe);
// { name: "Alice", age: 30 }
Filtering with entries() is safer than using delete on the original object — it produces a new object without sensitive fields, leaving the source untouched. The filter callback receives both the key and the value, so you can filter by key name, value type, or any combination of the two.
Converting query string to object
function parseQuery(queryString) {
const params = new URLSearchParams(queryString.slice(1));
return Object.fromEntries(params);
}
const result = parseQuery("?sort=asc&limit=10&offset=20");
console.log(result);
// { sort: "asc", limit: "10", offset: "20" }
Parsing query strings with URLSearchParams + fromEntries() is concise, but remember that all values are strings. If you need numbers or booleans, add a type-coercion step between entries() and fromEntries() or use a dedicated query-string library for production code.
Object to Map and back
const obj = { a: 1, b: 2 };
const map = new Map(Object.entries(obj));
map.set("c", 3);
const backToObj = Object.fromEntries(map);
console.log(backToObj);
// { a: 1, b: 2, c: 3 }
Error cases
fromEntries throws a TypeError if the argument is not iterable, or if any item in the iterable does not expose a [Symbol.iterator] on its entries. Passing null, undefined, or a number directly will throw. Each element of the iterable must itself be iterable (a two-element array is the common case), but any iterable yielding two values works.
Key requirements
fromEntries expects each item in the iterable to be an array (or iterable) with at least two elements: [key, value]. The key is converted to a string (same as object property assignment), so non-string keys like numbers or symbols become string keys:
const entries = [[1, 'one'], [2, 'two']];
const obj = Object.fromEntries(entries);
console.log(obj); // { '1': 'one', '2': 'two' }
If you need non-string keys, use a Map instead of a plain object.
Duplicate keys
When the iterable contains duplicate keys, the last value wins — the same behaviour as assigning properties on a plain object one at a time:
const pairs = [['a', 1], ['a', 2], ['a', 3]];
console.log(Object.fromEntries(pairs)); // { a: 3 }
The last-value-wins behaviour means fromEntries() acts like a property assignment loop — each [key, value] pair is set on the result in iteration order, and later pairs with the same key silently overwrite earlier ones.
fromEntries() as the inverse of entries()
The round-trip Object.fromEntries(Object.entries(obj)) produces a shallow clone of obj. This is equivalent to Object.assign({}, obj) or { ...obj }, but can be useful when building transformation pipelines with entries():
const prices = { apple: 1.0, banana: 0.5, cherry: 2.0 };
// Filter keys, transform values, rebuild — all in one pipeline
const result = Object.fromEntries(
Object.entries(prices)
.filter(([, v]) => v < 1.5)
.map(([k, v]) => [k, v * 1.1])
);
console.log(result); // { apple: 1.1, banana: 0.55 }
Browser and runtime support
Object.fromEntries() is available in all modern browsers and in Node.js 12+. It is not available in Internet Explorer. If you need to target environments without native support, a polyfill is straightforward: reduce the iterable into an empty object, assigning each [key, value] pair as a property. Most projects running ES2019+ syntax in a bundled build get native support automatically.
See Also
Object.entries()— Returns an array of key-value pairs from an object (inverse of fromEntries)Object.keys()— Returns an array of object keys, commonly used with fromEntries for transformations