An interesting take on reduce and Object.assign
Tom
Posted on June 20, 2021
In this article Why using object spread with reduce probably a bad idea I saw this strange line of code:
someArray.reduce(Object.assign, {})
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 }
Ah, good to know, might be handy sometime!
💖 💪 🙅 🚩
Tom
Posted on June 20, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.