Object.fromEntries()
ES2019+Transforms a list of key-value pairs into an object.
Syntax
Object.fromEntries(iterable)Parameters
iterable Iterable An iterable such as Array or Map containing key-value pairs
Return Value
A new object whose properties are given by the entries
Examples
const entries = [['a', 1], ['b', 2]];
console.log(Object.fromEntries(entries)); 📌 When to Use
Use Object.fromEntries() when you need to convert iterables of key-value pairs back into objects. Essential for transforming objects after filtering/mapping with Object.entries(), converting Maps to plain objects, or parsing URL query parameters.
⚠️ Common Mistakes
Passing an array without proper [key, value] structure - each element must be a two-element array or the conversion will fail.
Forgetting that duplicate keys will be overwritten - the last occurrence wins, just like object literal behavior.
Not handling non-string keys - Object.fromEntries() converts all keys to strings, which may cause unexpected behavior with Symbol or number keys.
✅ Best Practices
Use the entries-transform-fromEntries pattern for immutable object updates: Object.fromEntries(Object.entries(obj).map(...))
Parse URL parameters elegantly: Object.fromEntries(new URLSearchParams(queryString))
Convert Map to object when you need JSON serialization or object-specific operations.
⚡ Performance Notes
Object.fromEntries() has O(n) time complexity where n is the number of entries. It is efficient for most use cases but creates a new object on each call. When chained with Object.entries() transformations, be aware of the combined overhead of creating intermediate arrays plus the final object.
🌍 Real World Example
URL Query String Parser
Parse URL query parameters into a configuration object and filter sensitive data using Object.fromEntries().
const url = 'https://example.com?page=1&sort=date&token=abc123&limit=10';
const queryString = url.split('?')[1];
// Parse query string to object
const params = Object.fromEntries(new URLSearchParams(queryString));
console.log(params);
// { page: '1', sort: 'date', token: 'abc123', limit: '10' }
// Filter out sensitive parameters
const sensitiveKeys = ['token', 'password', 'secret'];
const safeParams = Object.fromEntries(
Object.entries(params).filter(([key]) => !sensitiveKeys.includes(key))
);
console.log(safeParams);
// { page: '1', sort: 'date', limit: '10' }
// Convert string numbers to actual numbers
const typedParams = Object.fromEntries(
Object.entries(safeParams).map(([key, value]) => [
key,
!isNaN(value) ? Number(value) : value
])
);
console.log(typedParams);
// { page: 1, sort: 'date', limit: 10 }