Hiprup

What is Object.fromEntries()?

Object.fromEntries() builds an object from a list of [key, value] pairs — the inverse of Object.entries().

  • From a Map — convert a Map straight into a plain object.

  • From transformations — pair with entries() + map/filter to transform objects.

  • From query params — turn URLSearchParams into an object.

Natural pairing: use it with Object.entries to map or filter an object and rebuild it.

// From entries array
const entries = [['name', 'John'], ['age', 30], ['city', 'NYC']];
const obj = Object.fromEntries(entries);
console.log(obj); // { name: 'John', age: 30, city: 'NYC' }

// From Map
const map = new Map([['a', 1], ['b', 2]]);
const objFromMap = Object.fromEntries(map);
console.log(objFromMap); // { a: 1, b: 2 }

// Transform object values (entries → map → fromEntries)
const prices = { apple: 1.5, banana: 0.75, cherry: 3.0 };
const discounted = Object.fromEntries(
  Object.entries(prices).map(([key, val]) => [key, val * 0.9])
);
console.log(discounted); // { apple: 1.35, banana: 0.675, cherry: 2.7 }

// From URLSearchParams
const params = new URLSearchParams('name=John&age=30');
const paramsObj = Object.fromEntries(params);
console.log(paramsObj); // { name: 'John', age: '30' }

// Inverse of Object.entries
const original = { x: 1, y: 2 };
const roundTrip = Object.fromEntries(Object.entries(original));
console.log(roundTrip); // { x: 1, y: 2 }

fromEntries converts [key, value] pairs to an object. The transform pipeline (entries → map → fromEntries) is the most powerful pattern: extract entries, transform them, and rebuild the object.

URLSearchParams to object is a practical web development use case. It is the exact inverse of Object.entries.

The entries → map → fromEntries pipeline for object transformation is the key pattern. Show the URLSearchParams conversion for practical value.

Know it is the inverse of Object.entries. ES2019 — know the year.

What is Object.fromEntries()? | Hiprup