<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Will</title>
    <description>The latest articles on DEV Community by Will (@pace0033).</description>
    <link>https://dev.to/pace0033</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F727398%2F50bb56e2-057e-44d0-8fe2-021e9bf20817.png</url>
      <title>DEV Community: Will</title>
      <link>https://dev.to/pace0033</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pace0033"/>
    <language>en</language>
    <item>
      <title>Demystifying Array.prototype.reduce()</title>
      <dc:creator>Will</dc:creator>
      <pubDate>Sat, 16 Oct 2021 18:45:05 +0000</pubDate>
      <link>https://dev.to/pace0033/demystifying-arrayprototypereduce-1hj8</link>
      <guid>https://dev.to/pace0033/demystifying-arrayprototypereduce-1hj8</guid>
      <description>&lt;h1&gt;
  
  
  Array.prototype.reduce()
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce"&gt;reduce()&lt;/a&gt; is considered by some Javascript beginners to be one of the most confusing array methods. In this tutorial I will present the method in a way that is easily understood, so you can start experimenting with &lt;code&gt;reduce()&lt;/code&gt; in your own code. We'll start by taking a macroscopic view of the method, then break it down into digestible bite-sized chunks to understand how each individual part works.&lt;/p&gt;

&lt;h2&gt;
  
  
  Description
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;reduce()&lt;/code&gt; is an Array method that executes a "reducer" &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Callback_function"&gt;callback function&lt;/a&gt; on each element of the target array. &lt;strong&gt;The "reducer" callback function must return a single value&lt;/strong&gt; for use in the next callback method on each subsequent array element.&lt;/p&gt;

&lt;h3&gt;
  
  
  Returns
&lt;/h3&gt;

&lt;p&gt;After &lt;code&gt;reduce()&lt;/code&gt; iterates over the last array element, the method returns the result of the final "reducer" callback function.&lt;/p&gt;

&lt;p&gt;I personally interpret that the method is called &lt;code&gt;reduce&lt;/code&gt; because it will iterate over every individual element of an array, but ultimately the method only returns one single value after traversing the entire array.&lt;/p&gt;

&lt;h3&gt;
  
  
  Destructive?
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;reduce()&lt;/code&gt; method itself does not directly mutate the array it is called on, so it is &lt;em&gt;not&lt;/em&gt; considered a destructive method. However, it's important to note that the callback function might call destructive methods that could mutate the original array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax
&lt;/h2&gt;

&lt;p&gt;The syntax of the &lt;code&gt;reduce()&lt;/code&gt; function itself is &lt;em&gt;very&lt;/em&gt; simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: The second parameter, &lt;code&gt;initialValue&lt;/code&gt;, is optional and not required for the function to run. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As you can see from above, the syntax to implement &lt;code&gt;reduce()&lt;/code&gt; is not confusing at all. I found the complexity of using &lt;code&gt;reduce()&lt;/code&gt; comes from the requirements of the "reducer" &lt;code&gt;callbackFn&lt;/code&gt; that is passed into the method. So, let's dive into the syntax of the callback function now.&lt;/p&gt;

&lt;p&gt;Here is an example "reducer" callback function that will sum all elements of an array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;reducer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;currentElement&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;accumulator&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;currentElement&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: Similar to other array methods, it is possible to pass two additional parameters to the callback function. The 3rd parameter passed represents the current index of the array, and the 4th parameter represents the array being traversed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here's how each element works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;accumulator&lt;/code&gt;: This parameter "accumulates" the results of each execution of the callback function. The value that is returned by the preceding callback function becomes the accumulator value in each execution of the callback function. &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: By default if no &lt;code&gt;initialValue&lt;/code&gt; is passed in the &lt;code&gt;reduce()&lt;/code&gt; function, then &lt;code&gt;accumulator&lt;/code&gt; is initially set to equal the first element of the array. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;currentElement&lt;/code&gt;: This parameter represents the value of the current array element that is being iterated over.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Still confused?
&lt;/h4&gt;

&lt;p&gt;Not to worry, let's go into a simple example together and I'll explain how all of these moving pieces work together in the &lt;code&gt;reduce()&lt;/code&gt; method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example Breakdown
&lt;/h2&gt;

&lt;p&gt;The most basic implementation of &lt;code&gt;reduce()&lt;/code&gt; is to return the sum of all the elements in an array. To start, let's sum up the following odd numbers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;reducer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;currentElement&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;accumulator&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;currentElement&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code snippet will "reduce" the array of numbers into a single number by adding them together. The expected result is &lt;code&gt;16&lt;/code&gt; because &lt;code&gt;1 + 3 + 5 + 7 = 16&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's break this example down to make it more simple.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To start, we call &lt;code&gt;reduce&lt;/code&gt; on the &lt;code&gt;numbers&lt;/code&gt; array and we pass in the callback function &lt;code&gt;reducer&lt;/code&gt; as a parameter into &lt;code&gt;reduce&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;We did not pass the 2nd optional parameter, &lt;code&gt;initialValue&lt;/code&gt;, into the &lt;code&gt;reduce()&lt;/code&gt; function. So, for the first execution of &lt;code&gt;reducer&lt;/code&gt; the &lt;code&gt;accumulator&lt;/code&gt; is set to the value of the first element in the array and &lt;code&gt;currentElement&lt;/code&gt; is set to the value of the second element in the array.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's what the first call of &lt;code&gt;reduce()&lt;/code&gt; looks like with the &lt;code&gt;reducer&lt;/code&gt; callback parameters replaced with array elements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now written with the values in place of the parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the initial call of the &lt;code&gt;reducer&lt;/code&gt; callback function, &lt;code&gt;reduce()&lt;/code&gt; iterates to the next array element executing the &lt;code&gt;reducer&lt;/code&gt; callback function over and over until it reaches the end of the array. &lt;/p&gt;

&lt;p&gt;Here's a breakdown of the next call of the &lt;code&gt;reducer&lt;/code&gt; callback function. This time &lt;code&gt;accumulator&lt;/code&gt; is set to equal the returned result of the previous callback function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now written with the values in place of the parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Are you getting the pattern yet? The &lt;code&gt;accumulator&lt;/code&gt; simply accumulates the result of the previous callback function and uses it in the next execution of the callback. So for our final call of the example, the &lt;code&gt;accumulator&lt;/code&gt; will be equal to &lt;code&gt;9&lt;/code&gt; as that is the returned value of the previous callback function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now written with the values in place of the parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This was the final call of the &lt;code&gt;reducer&lt;/code&gt; callback function because we have now iterated over each array element, so &lt;code&gt;16&lt;/code&gt; will be the value returned from the original &lt;code&gt;reduce&lt;/code&gt; method called on the &lt;code&gt;numbers&lt;/code&gt; array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other Uses of reduce()
&lt;/h2&gt;

&lt;p&gt;As you saw from the example above, &lt;code&gt;reduce()&lt;/code&gt; is very effective at returning the sum of all elements in an array. You might be wondering what other practical uses exist for &lt;code&gt;reduce()&lt;/code&gt;. Here are a few:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sum values in an object array&lt;/li&gt;
&lt;li&gt;Flatten an array of arrays&lt;/li&gt;
&lt;li&gt;Replace .filter().map() &lt;/li&gt;
&lt;li&gt;And more!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Challenge
&lt;/h2&gt;

&lt;p&gt;Want more practice? Try to code the following challenge:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Using reduce(), write an implementation that will return the sum of all even numbers in an array. 

Hint: You must use a conditional statement in your callback function.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
