DEV Community

Discussion on: The beast that is Array.prototype.reduce

Collapse
 
tompearson profile image
Tom Pearson

I'm a big fan of reduce but there's often a simpler solution, or at least one which makes for more legible code. For example you can flatten an array of arrays using concat and restructuring e.g.

const a = [[1,2,3,4,5],[1,2,3,4,5]];

const flattened = [].concat(...a);
// flattened will be ... [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

As a general policy I try to name my reducer function arguments to reflect what they're doing in the specific case rather than just giving them general names (as you do in your examples i.e. map vs acc for the 'previousValue' argument).