DEV Community

Discussion on: You Might Not Need Lodash

Collapse
 
gsonderby profile image
Gert Sønderby

The array I am pushing to is the accumulator of the reduce call, though. I'm pushing the elements of each array found, once flattened.

It works like a classic head/tail recursion, if an element is an array it first gets flattened itself, then it's elements are pushed onto the accumulator.

Thread Thread
 
masaeedu profile image
Asad Saeeduddin

That's fine, but as I said, it's confusing to use reduce and mutation simultaneously. You're passing [] as a seed value, so the only thing that got mutated was that seed array. If you'd used array.reduce((flatArray, item) => ...) without the seed value (which would be fine but for the mutation), it would end up filling the first item in the input array you were passed.

In general it's easier on the fallible human developer to make mutation look like mutation and pure computation look like pure computation.

Thread Thread
 
gsonderby profile image
Gert Sønderby

The only way it would matter here is if you somehow changed the function to make flatArray available while the function was running. I'm not even sure how you'd do that. But I do want to point your attention to the words, used above: "quick and dirty"...