DEV Community

A Quick JS Reduction: My possibly-too-clever way to reduce an array into one object in javascript

Boots on April 20, 2020

Let's say you have an array of user objects, all with unique IDs, but you want to group them in a single object by their ids. // These are your o...
Collapse
 
avalander profile image
Avalander

Not that it matters, but for the sake of offering an alternative solution I'd probably lean more towards Object.fromEntries than reduce.

Object.fromEntries(users.map(u => [ u.id, u ]))
Enter fullscreen mode Exit fullscreen mode
Collapse
 
brettimus profile image
Boots

totally matters and I wish I could heart this twice -- I've never seen Object.fromEntries!