As humans, we adapt continuously … ingesting data, processing it, acting on it, and producing a result. It’s never a 1:1… Hot + Sunny doesn’t necessarily mean T-Shirt + Shades. Sometimes it just means Air Conditioning. When we look at complex data, sometimes we want to perform analysis and operations on it, to roll it up into something new. Often, we need the list of data points as input to produce something totally different on the other side … Adapting it to solve a problem, while reducing the noise.
We do the same thing in code. When we need to pull the substance out of a noisy data set in JavaScript, we turn to the reduce() method.
What is the JS Array Reduce Method?
As per MDN:
Array.prototype.reduce() : executes a reducer function (that you provide) on each element of the array, resulting in single output value.
That means we get a new, likely smaller, array with just the values meeting the conditions we specify. Great!
How do I use it?
Let’s see it in action!