DEV Community

Discussion on: You Might Not Need Lodash

Collapse
 
gsonderby profile image
Gert Sønderby

Your first example chokes on [1,[2,3,[4]],5,[6,7]], throwing an error. Worse, on ['yo',['dawg','I',['herd']],'you',['like','arrays']] it breaks in a number of interesting ways, spreading some strings, failing to spread some arrays.

Thread Thread
 
masaeedu profile image
Asad Saeeduddin

It is an implementation of the join function for arrays, and works with arrays of arrays, not arrays of mixed objects.

If you need it to work with mixed objects you'll need a recursive call (or ap+pure):

const flattened = as => as.reduce((p, c) => [...p, ...(Array.isArray(c) ? flattened(c) : [c])], [])

Anyway, the point is to avoid mutation in the reduce, whatever it is you may be implementing.

Thread Thread
 
gsonderby profile image
Gert Sønderby

At this point it honestly seems like you're just trying to score points against me, in some way. I am disinterested in continuing that.

Thread Thread
 
masaeedu profile image
Asad Saeeduddin

Sorry to hear you feel that way. I was just suggesting that using mutation in reduce is confusing, didn't mean for it to turn into this long-running back and forth.