Reduce is a very versatile and powerful tool for controlling data structures. The general structure of one as explained here is:
Reduce runs through each item in the array and lets you control what happens in the iteration. The available arguments to use are:
accis the “accumulated value,” or the one that carries over from each item in the array. It’s starting value is set in
initialValue, and it can be used as a reference point or changed in different ways throughout the reduce.
valueis the value of the current item in the iteration.
indexis the current item’s index
arrayis the array being reduced
A simple example the article also lists is using it to get the sum of an array
0 is the default starting value for
acc. It can be any number, value, or object. If you’re iterating to add key/value pairs, for instance, you can set it to
You can also use it to return an array based on if items are even or not