<?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: hannybananny</title>
    <description>The latest articles on DEV Community by hannybananny (@hannybananny).</description>
    <link>https://dev.to/hannybananny</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%2F830766%2F092286a5-a3d8-46c6-af65-cafe435262fe.png</url>
      <title>DEV Community: hannybananny</title>
      <link>https://dev.to/hannybananny</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hannybananny"/>
    <language>en</language>
    <item>
      <title>About the .reduce Method</title>
      <dc:creator>hannybananny</dc:creator>
      <pubDate>Tue, 31 May 2022 01:56:05 +0000</pubDate>
      <link>https://dev.to/hannybananny/about-the-reduce-method-2d1</link>
      <guid>https://dev.to/hannybananny/about-the-reduce-method-2d1</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hi, I am Hannah.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Novice coder learning through writing.&lt;/p&gt;

&lt;p&gt;Any constructive input or clarifications welcome :)&lt;/p&gt;

&lt;h2&gt;
  
  
  What does .reduce do?
&lt;/h2&gt;

&lt;p&gt;The .reduce method takes in an array and performs a reducer function callback, written by the user, on each element of the array. The final result of the .reduce method is a single value. This value may be an integer, string, object, ect.&lt;/p&gt;

&lt;p&gt;The .reduce method is non-destructive to the original array. &lt;/p&gt;

&lt;h2&gt;
  
  
  How to build the .reduce function
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Parameters
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Callback function&lt;/strong&gt;&lt;br&gt;
This is the reducer function supplied by the user.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Initial Value (optional)&lt;/strong&gt;&lt;br&gt;
This is our starting point. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We can then formulate the basic structure of our .reduce method as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array.reduce( () = &amp;gt; {

}, 0)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The arrow function will become the user-supplied &lt;strong&gt;callback function&lt;/strong&gt;, and we have set the &lt;strong&gt;initial value&lt;/strong&gt; at 0.&lt;/p&gt;

&lt;h2&gt;
  
  
  More on the Callback function
&lt;/h2&gt;

&lt;p&gt;The callback function takes 4 parameters.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Accumulator&lt;/strong&gt; - (required)&lt;br&gt;
This is the value that we want to reduce our array to. &lt;br&gt;
For the code above, we want to find the &lt;em&gt;total&lt;/em&gt; fish caught.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Current Value&lt;/strong&gt; - (required)&lt;br&gt;
The element of the array that we are currently looping through. For the array above, we want to loop through each individual fish.&lt;/p&gt;

&lt;p&gt;I will be using the following array to demonstrate how the .reduce method works. With this array, I would like to find the total amount of fish caught.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let fish= [
  {name: 'Halibut', amount: 100},
  {name: 'Pacific Cod', amount: 10},
  {name: 'Yellow Irish Lord', amount:14},
  {name: 'Rougheye', amount: 22}
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The start to our function will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let totalFish = fish.reduce((total, fish) =&amp;gt; {

}, 0)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Current Index&lt;/strong&gt; - (optional)&lt;br&gt;
This is the the current index of the current value in the function. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Array&lt;/strong&gt; - (optional)&lt;br&gt;
This is the array we are iterating through. In the sample above, it would be the fish array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let num1 = fish.reduce((total, fish, index, array) =&amp;gt; {

}, 0)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Typically, when coming across the .reduce function, we will only see the required parameters (accumulator and current value). We will see the optional parameters occassionally, as needed by the developer. &lt;/p&gt;

&lt;h3&gt;
  
  
  Return
&lt;/h3&gt;

&lt;p&gt;This is the value that we want to have returned upon completion of running the reducer function through the entire array.&lt;br&gt;
&lt;strong&gt;You must return something for the .reduce function to work.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let totalFish = fish.reduce((total, fish) =&amp;gt; {
  return total + fish.amount
}, 0)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What is happening in our code?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7lSzJ4Zj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zb1p0cmpqzl99mm7yjxd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7lSzJ4Zj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zb1p0cmpqzl99mm7yjxd.png" alt="Image description" width="880" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First Loop&lt;/strong&gt; - Our total (i.e. accumulator) equals 0, what we set our initial value to. Our fish amount equals 100.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Second Loop&lt;/strong&gt; - Our total equals 100, which is the sum of our inital value + the previous fish amount. Our fish total equals 10. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Third Loop&lt;/strong&gt; - Our total equals 110, which is the sum of the second loop total + the previous fish amount. Our fish total equals 14.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fourth Loop&lt;/strong&gt; - Our total equals 124, which is the sum of the third loop total + the previous fish amount. Our fish amount is 22.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final Value&lt;/strong&gt; - Our total equals 146, which is the sum of the fourth loop total + the previous fish amount. We have finished our iteration. &lt;/p&gt;

&lt;p&gt;For each iteration, our total becomes what is returned from the previous iteration. &lt;/p&gt;

&lt;h2&gt;
  
  
  More about the initial value
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What happens if we do not set an initial value?&lt;/strong&gt;**&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VQB598gP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ts9zak3qnvcaifn41yy8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VQB598gP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ts9zak3qnvcaifn41yy8.png" alt="Image description" width="880" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our numbers.reduce method acts as expected. We expect our function to take each number in the array and multiply it by 3 to get a final value of 183.&lt;/p&gt;

&lt;p&gt;Our numbers2.reduce method does not act as expected. Because we did not set our initial value, the reduce method sets the total as the value at index[0], 5 in this case, and our number begins at 11 for our first iteration. We do not multiply 5 by 3, so our expected value is 10 less than expected. &lt;/p&gt;

&lt;p&gt;It is important to set an initial value to avoid errors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Returning an Object
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WuxD7NFS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ejoh4ncyvnz1lg67f98.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WuxD7NFS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ejoh4ncyvnz1lg67f98.png" alt="Image description" width="880" height="375"&gt;&lt;/a&gt;&lt;br&gt;
Above we have an array with different fish caught, we want to keep a tally of these fish and return it as an object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Writing our reducer function&lt;/strong&gt;&lt;br&gt;
Our accumulator is our tally, our current value is our fish.&lt;br&gt;
Our if statement is stating, &lt;em&gt;if&lt;/em&gt; our current fish is not in our object, we will add the fish to the object with a tally value of 1. &lt;br&gt;
&lt;em&gt;Else&lt;/em&gt;, if our current fish is in our object, we will increment the tally by 1. &lt;br&gt;
We are then returning the tally.&lt;br&gt;
It is very important to remember to &lt;strong&gt;return&lt;/strong&gt; the tally for the function to work.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Calling the reducer function&lt;/strong&gt;&lt;br&gt;
We are setting the .reduce method to the variable tallyFish. &lt;br&gt;
Then, we call the .reduce method on the fish array with the first parameter being our reducer function and our second parameter (initial value) is an empty object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Result&lt;/strong&gt;&lt;br&gt;
The tally (our accumulator) is updated through every iteration of our array. When we console.log our variable, tallyFish, we see the final result. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Above are the basics of the .reduce method in a nutshell.&lt;/strong&gt;&lt;br&gt;
The following resources provide more in-depth coverage of this topic, and a very useful video. &lt;br&gt;
Thanks for reading! &lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=s1XVfm5mIuU"&gt;https://www.youtube.com/watch?v=s1XVfm5mIuU&lt;/a&gt;&lt;br&gt;
&lt;a href="https://egghead.io/lessons/javascript-reduce-an-array-into-a-single-object"&gt;https://egghead.io/lessons/javascript-reduce-an-array-into-a-single-object&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
