I was recently mentoring someone who had trouble with the 1 method in JavaScript. Namely, how you get from this: Show
…to this:
They are functionally equivalent and they both sum up all the numbers in the array, but there is a bit of paradigm shift between them. Let’s explore reducers for a moment because they’re powerful, and important to have in your programming toolbox. There are literally hundreds of other articles on reducers out there, and I’ll link up some of my favorites at the end. What is a reducer?The first and most important thing to understand about a reducer is that it will always only return one value. The job of a reducer is to reduce. That one value can be a number, a string, an array or an object, but it will always only be one. Reducers are really great for a lot of things, but they’re especially useful for applying a bit of logic to a group of values and ending up with another single result. That’s the other thing to mention: reducers will not, by their nature, mutate your initial value; rather they return something else. Let’s walk over that first example so you can see what’s happening here. The video below explains: Your browser does not support the video tag. It might be helpful to watch the video to see how the progression occurs, but here’s the code we’re looking at:
We have our array ( 2) and the first value each number in the array will be added to ( 3). We walk through the amount of the array and add them to the initial value.Let’s try this a little differently:
Now we have the same array, but this time we’re not mutating that first value. Instead, we have an 4 that will only be used at the start. Next, we can make a function that takes an accumulator
and an item. The accumulator is the collected value
returned in the last invocation that informs the function what the next value will be added to. In this case of addition, you can think of it as a snowball rolling down a mountain that eats up each value in its path as it grows in size by every eaten value.We’ll use 1 to apply the function and start from that initial value. This can be shortened with an arrow function:
And then shortened some more! Implicit returns for the win!
Now we can apply the function right where we called it, and we can also plop that initial value directly in there!
An accumulator can be an intimidating term, so you can think of it like the current state of the array as we’re applying the logic on the callback’s invocations. The Call StackIn case it’s not clear what’s happening, let’s log out what’s going on for each iteration. The reduce is using a callback function that will run for each item in the array. IThe following demo will help to make this more clear. I’ve also used a different array ( 6) because having the numbers be the same as the index could be confusing.See the Pen showing acc, item, return by Sarah Drasner (@sdras) on CodePen. When we run this, we’ll see this output in the console:
Here’s a more visual breakdown: Your browser does not support the video tag.
Simple ExamplesNow that we’ve got that under our belt, let’s look at some common and useful things reducers can do. How many of X do we have?Let’s say you have an array of numbers and you want to return an object that reports the number of times those numbers occur in the array. Note that this could just as easily apply to strings.
See the Pen simplified reduce by Sarah Drasner (@sdras) on CodePen. Wait, what did we just do? Initially, we have an array and the object we’re going to put its contents into. In our reducer, we ask: does this item exist? If so, let’s increment it. If not, add it and set it to 1. At the end, please return the tally count of each item. Then, we run the reduce function, passing in both the reducer and the initial value. Take an array and turn it into an object that shows some conditionsLet’s say we have an array and we want to create an object based on a set of conditions. Reduce can be great for this! Here, we want to create an object out of any instance of a number contained in the array and show both an odd and even version of this number. If the number is already even or odd, then that’s what we’ll have in the object.
See the Pen simplified reduce by Sarah Drasner (@sdras) on CodePen. This will shoot out the following output in the console: 0OK, so what’s happening? As we’re going through every item in the array, we create a property for even and odd, and based on an inline condition with a modulus operator, we’ll either store the number or increment it by 1. The modulus operator is really good for this because it can quickly check for even or odd — if it’s divisible by two, it’s even, if not, it’s odd. Other resourcesAt the top, I mentioned other posts out there that are handy resources to get more familiar with the role of reducers. Here are a few of my favorites: How to reduce CSS size?There are three basic optimizations you can make to limit the amount of data transferred in the delivery process:. CSS minification,. CSS file length reduction by reducing unnecessary code, and.. CSS compression to deliver a smaller file that the browser can still read.. What is the best CSS Minifier?Results of comparison #2:. How to convert normal CSS to minified?css-minify npm. First, install the tool using npm install css-minify -g.. To minify a single CSS file, type the following command: css-minify -f filename.. To minify all the css files of a directory, type: css-minify -d sourcedir. where sourcedir is the name of the folder containing the css files.. Why minimize CSS?Website owners mainly choose to minify CSS to increase their page speed. The basic principle is simple: The less code there is to process, the less time it takes to load the web page. This allows you to delight website visitors with fast load times.
|