<?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: Muhannad</title>
    <description>The latest articles on DEV Community by Muhannad (@man2006).</description>
    <link>https://dev.to/man2006</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%2F102781%2Fa45fe368-61b2-4f4d-9a07-5de60b1f7707.jpg</url>
      <title>DEV Community: Muhannad</title>
      <link>https://dev.to/man2006</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/man2006"/>
    <language>en</language>
    <item>
      <title>Map, Filter, &amp; Reduce in JavaScript</title>
      <dc:creator>Muhannad</dc:creator>
      <pubDate>Sat, 18 Jan 2020 01:45:34 +0000</pubDate>
      <link>https://dev.to/man2006/map-filter-reduce-in-javascript-4g3l</link>
      <guid>https://dev.to/man2006/map-filter-reduce-in-javascript-4g3l</guid>
      <description>&lt;p&gt;This post is a basic walkthrough of JavaScript's map, filter, and reduce methods that provides practical use cases of functional programming in JavaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  What do they all have in common?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;All three functions are array methods (i.e. methods you can call on your array variables).&lt;/li&gt;
&lt;li&gt;The returned value in each function is a new array containing the result of the operations performed on the original array in a function you provide.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Map
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;map()&lt;/code&gt;  method allows you iterate over each element in your array, and then manipulate each element in any desired way through the use of a provided function.&lt;/li&gt;
&lt;li&gt;The provided function be an anonymous function, or a named function.&lt;/li&gt;
&lt;li&gt;It is very important to note that you want to use the &lt;code&gt;map()&lt;/code&gt; method when you are expecting a return value from your function. Otherwise, it is recommended to use JavaScript's &lt;code&gt;forEach()&lt;/code&gt; method when you want to modify the original array.&lt;/li&gt;
&lt;li&gt;TL;DR - &lt;code&gt;map()&lt;/code&gt; is a &lt;code&gt;for&lt;/code&gt; loop with a return value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Basic Example&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is a simple example provided by the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map"&gt;MDN&lt;/a&gt; webpage on the &lt;code&gt;map()&lt;/code&gt; method
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const evenNumbers = [2, 4, 6]

const result = evenNumbers.map(currentValue =&amp;gt; currentValue * 2)

// result = [4, 8, 12]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Practical Use Case&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is an example you will often encounter where an API call returns an array of objects containing information about your users.&lt;/li&gt;
&lt;li&gt;The goal here is to extract the list of names from the response received from your API.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const users = [
{
    'name': 'John Doe',
    'address': 'Wellington St, Ottawa, ON K1A 0A9'
},
{
    'name': 'Jane Doe',
    'address': '1600 Pennsylvania Ave NW, Washington, DC 20500'
},
{
    'name': 'Hannibal Lecter',
    address: '10800 97 Ave NW, Edmonton, AB T5K 2B6'
}]

const names = users.map(currentItem =&amp;gt; {
    return currentItem['name']
})

// names = ['John Doe', 'Jane Doe', 'Hannibal Lecter']
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Filter
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Similar to the &lt;code&gt;map()&lt;/code&gt; method, the &lt;code&gt;filter()&lt;/code&gt; method allows you to iterate over the elements of your array.&lt;/li&gt;
&lt;li&gt;Although, the difference in this case is that &lt;code&gt;filter()&lt;/code&gt; returns values that pass some test case provided.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Basic Example&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is another simple example provided by the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter"&gt;MDN&lt;/a&gt; webpage on the &lt;code&gt;filter()&lt;/code&gt; method.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [2, 4, 6, 11, 12, 33]

const evenNumbers = numbers.filter(x =&amp;gt; x % 2 === 0)

// evenNumbers = [2, 4, 6, 12]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Practical Use Case&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Similar to the previous example, this is a situation where an API call returns an objects containing information about your users.&lt;/li&gt;
&lt;li&gt;The goal is to extract the names of users living outside the United States from the response received back from your API.&lt;/li&gt;
&lt;li&gt;The goal is to extract the names of users living outside of the United States form the response received back from your API.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const users = [
{
    'name': 'John Doe',
    'address': 'Wellington St, Ottawa, ON K1A 0A9',
    'country': 'Canada'
},
{
    'name': 'Jane Doe',
    'address': '1600 Pennsylvania Ave NW, Washington, DC 20500',
    'country': 'United States'

},
{
    'name': 'Hannibal Lecter',
    'address': '10800 97 Ave NW, Edmonton, AB T5K 2B6',
    'country': 'Canada'

}]

const expatriates = users.filter(currentItem =&amp;gt; {
    return currentItem['country'] !== 'United States'
})

/* expatriates = [
{
    name: 'John Doe',
    address: 'Wellington St, Ottawa, ON K1A 0A9',
    country: 'Canada'
},
{
    name: 'Hannibal Lecter',
    address: '10800 97 Ave NW, Edmonton, AB T5K 2B6',
    country: 'Canada'
}]
*/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Using this code snippet alone will not suffice if we are attempting to obtain an array containing names alone.&lt;/li&gt;
&lt;li&gt;Therefore, building on the previous knowledge we acquired about the &lt;code&gt;map()&lt;/code&gt; method, we will need one more function call.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const expatriateNames = expatriates.map(currentValue =&amp;gt; {
    return currentValue['name']
})

// expatriateNames = ['John Doe', 'Hannibal Lecter']
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Reduce
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;As the name suggests, the &lt;code&gt;reduce()&lt;/code&gt; method will accept an array containing multiple values and "reduce" them down into a single return value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    array.reduce((accumulator, current) =&amp;gt; {}, initialValue)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Basic Example&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In this particular example, we are attempting to iterate over the elements of an array and sum them up.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [1, 2, 3]

const result = array.reduce((previousValue, currentValue) =&amp;gt; {
    return previousValue + currentValue
})

// result = 6
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Practical Use Case&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The previous practical use case provided for the &lt;code&gt;filter()&lt;/code&gt; is less than ideal as we had to rely on two different function calls to obtain our desired result.&lt;/li&gt;
&lt;li&gt;In this implementation, we have the same goal as previous, extract the list of names of users living outside the United States from our response received from our API.&lt;/li&gt;
&lt;li&gt;In this case, we can reduce both &lt;code&gt;filter()&lt;/code&gt; and &lt;code&gt;map()&lt;/code&gt; method calls into a single function using the &lt;code&gt;reduce()&lt;/code&gt; method.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const users = [
{
    'name': 'John Doe',
    'address': 'Wellington St, Ottawa, ON K1A 0A9',
    'country': 'Canada'
},
{
    'name': 'Jane Doe',
    'address': '1600 Pennsylvania Ave NW, Washington, DC 20500',
    'country': 'United States'

},
{
    'name': 'Hannibal Lecter',
    'address': '10800 97 Ave NW, Edmonton, AB T5K 2B6',
    'country': 'Canada'

}]

const expatriates = users.reduce((result, user) =&amp;gt; {
    // filter() equivalent
    if (user['country'] === 'United States') {
        return result
    }

    // map() equivalent
    result.push(user['name'])
    return result
}, initialValue = [])
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In this implementation, we do not add any value to the accumulator array, in this case we have called it &lt;code&gt;result&lt;/code&gt;, thus successfully filtering out users who live outside the United States.&lt;/li&gt;
&lt;li&gt;Next, once we have passed the initial &lt;code&gt;if&lt;/code&gt; statement check, we add the name of the user to the &lt;code&gt;accumulator&lt;/code&gt; array.&lt;/li&gt;
&lt;li&gt;It is important that note that while you are able to return a single output from a reduce function, that return value can be an array by initializing the initial value to an empty array.&lt;/li&gt;
&lt;li&gt;Perhaps, my &lt;a href="https://www.youtube.com/watch?v=tVCYa_bnITg"&gt;favourite explanation&lt;/a&gt; of the &lt;code&gt;reduce()&lt;/code&gt; method suggested thinking of the &lt;code&gt;accumulator&lt;/code&gt; as the previous value.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Credits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;This post would not have been possible without these wonderful tools and content creators

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/"&gt;Mozilla Developer Network&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://fireship.io/"&gt;Fireship&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://repl.it/"&gt;Repl.it&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
