DEV Community

loading...

Discussion on: Transforming data with Array.reduce

Collapse
lukaszahradnik profile image
Lukáš Zahradník • Edited

Hi,

I would recommend using approach bellow instead of yours (with object destructuring)

const toObject = data.reduce((acc, item) => {
   acc[item.id] = item
   return acc
}, {})
Enter fullscreen mode Exit fullscreen mode

Your approach creates a new object on every iteration which is just more time consuming in the end.

The same applies for the array - use push instead of destructuring.