DEV Community

Tom
Tom

Posted on

1 1

An interesting take on reduce and Object.assign

In this article Why using object spread with reduce probably a bad idea I saw this strange line of code:

someArray.reduce(Object.assign, {})
Enter fullscreen mode Exit fullscreen mode

I thought Object.assign was meant for merging objects. Why the reduce? What is going on here??

If I want to test some JavaScript I run node in a terminal to create a playfield:

[1, 2, 3].reduce(Object.assign, {})

// output:
{ '0': 1, '1': 2, '2': 3 }
Enter fullscreen mode Exit fullscreen mode

Ah, good to know, might be handy sometime!

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up