<?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: Holajuwon</title>
    <description>The latest articles on DEV Community by Holajuwon (@holajuwon).</description>
    <link>https://dev.to/holajuwon</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%2F248226%2Fc7423c96-632f-4af5-9b88-5d61925fd55e.png</url>
      <title>DEV Community: Holajuwon</title>
      <link>https://dev.to/holajuwon</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/holajuwon"/>
    <language>en</language>
    <item>
      <title>Understanding Array Methods: filter(), map(), forEach() 
</title>
      <dc:creator>Holajuwon</dc:creator>
      <pubDate>Wed, 06 Oct 2021 03:21:11 +0000</pubDate>
      <link>https://dev.to/holajuwon/understanding-array-methods-filter-map-foreach-3367</link>
      <guid>https://dev.to/holajuwon/understanding-array-methods-filter-map-foreach-3367</guid>
      <description>&lt;p&gt;Arrays are one of the most popular data types used in javascript as they have variety of methods that makes it easy to use. &lt;/p&gt;

&lt;p&gt;In this article, I will be talking about three popular array methods; the &lt;code&gt;filter()&lt;/code&gt; method, the &lt;code&gt;map()&lt;/code&gt; method and the &lt;code&gt;forEach()&lt;/code&gt; method,and I will be showing how they are used.&lt;/p&gt;

&lt;h2&gt;
  
  
  filter()
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;filter()&lt;/code&gt; method is mostly used for returning a subset of an array that meets a certain condition. &lt;/p&gt;

&lt;h3&gt;
  
  
  How its used
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;currentValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// condition&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;filter()&lt;/code&gt; method accepts a callback function.&lt;/li&gt;
&lt;li&gt;The callback takes in three positional arguments.

&lt;ul&gt;
&lt;li&gt;The first is the &lt;code&gt;currentValue&lt;/code&gt;: which specifies the current element in the array being looped over. The &lt;code&gt;currentvalue&lt;/code&gt; argument is required, i.e the value must be passed into the callback function.&lt;/li&gt;
&lt;li&gt;The second is the &lt;code&gt;index&lt;/code&gt;: which specifies the index of the current element in the array, this is an optional argument.&lt;/li&gt;
&lt;li&gt;The third is the present &lt;code&gt;array&lt;/code&gt; which the &lt;code&gt;filter()&lt;/code&gt; method is applied on, this is an optional argument.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;The callback takes in a condition to test all the elements in the array and return values based on the condition.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Return even numbers from an array of numbers&lt;/strong&gt;&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;let&lt;/span&gt; &lt;span class="nx"&gt;arr&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="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="mi"&gt;6&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="mi"&gt;8&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;10&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// result =&amp;gt; [2, 4, 6, 8, 10]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How it works
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;filter()&lt;/code&gt; method loops over every element in the array.&lt;/li&gt;
&lt;li&gt;It checks if the element pass the set condition; if it has a remainder after dividing two in this case.&lt;/li&gt;
&lt;li&gt;It passes it to a new array.&lt;/li&gt;
&lt;li&gt;When it has checked all the elements, it returns a new array that contains elements that pass the set condition.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Note:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;filter()&lt;/code&gt; method does not mutate the array.&lt;/li&gt;
&lt;li&gt;The method returns a new array with all the elements that passed the condition set.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  map()
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;map()&lt;/code&gt; method enables us to loop over every element of an array and perform various operations on them.&lt;br&gt;
It returns a new array with the new values of the elements after the operations have been carried out on them.&lt;/p&gt;
&lt;h3&gt;
  
  
  How its used
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;currentValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// operation&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;map()&lt;/code&gt; method accepts a callback function.&lt;/li&gt;
&lt;li&gt;The callback takes in three positional arguments.

&lt;ul&gt;
&lt;li&gt;The first is the &lt;code&gt;currentValue&lt;/code&gt;: which specifies the current element in the array being looped over. The &lt;code&gt;currentvalue&lt;/code&gt; argument is required, i.e the value must be passed into the callback function.&lt;/li&gt;
&lt;li&gt;The second is the &lt;code&gt;index&lt;/code&gt;: which specifies the index of the current element in the array, this is an optional argument.&lt;/li&gt;
&lt;li&gt;The third is the present &lt;code&gt;array&lt;/code&gt; which the &lt;code&gt;map()&lt;/code&gt; method is applied on, this is an optional argument.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The callback function allow us to perform various operations on the elements in the array.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Return all texts in an array in uppercase&lt;/strong&gt;&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;let&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Dog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Cat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;HORSE&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;elEPHANT&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;// result =&amp;gt; ['DOG', 'CAT', 'HORSE', 'ELEPHANT']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How it works
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;map()&lt;/code&gt; method loops over every element in the array.&lt;/li&gt;
&lt;li&gt;It picks each element and performs the stated operation on it; in this case it converts each element to uppercase.&lt;/li&gt;
&lt;li&gt;It passes it to a new array.&lt;/li&gt;
&lt;li&gt;When it has worked on all elements, it returns a new array that contains all elements in uppercase.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Note:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;map()&lt;/code&gt; method does not mutate the array.&lt;/li&gt;
&lt;li&gt;The method returns a new array with the results of the operation in the callback function.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  forEach()
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;forEach()&lt;/code&gt; method is mostly used to loop over each of the items in an array. It executes a provided function once for each array element. The &lt;code&gt;forEach()&lt;/code&gt; does not have a return value, it returns &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How its used
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;currentValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;currentValue&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;ul&gt;
&lt;li&gt;The &lt;code&gt;forEach()&lt;/code&gt; method accepts a callback function.&lt;/li&gt;
&lt;li&gt;The callback takes in three positional arguments.

&lt;ul&gt;
&lt;li&gt;The first is the &lt;code&gt;currentValue&lt;/code&gt;: which specifies the current element in the array being looped over. The &lt;code&gt;currentvalue&lt;/code&gt; argument is required, i.e the value must be passed into the callback function.&lt;/li&gt;
&lt;li&gt;The second is the &lt;code&gt;index&lt;/code&gt;: which specifies the index of the current element in the array, this is an optional argument.&lt;/li&gt;
&lt;li&gt;The third is the present &lt;code&gt;array&lt;/code&gt; which the &lt;code&gt;forEach()&lt;/code&gt; method is applied on, this is an optional argument.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;The callback function allow us to perform various operations on the elements in the array.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;As an alternative to the &lt;code&gt;for of&lt;/code&gt; loop&lt;/strong&gt;&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;let&lt;/span&gt; &lt;span class="nx"&gt;animals&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Dog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Cat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;HORSE&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;elEPHANT&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;// for of&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;animal&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;animals&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;animal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// forEach&lt;/span&gt;
&lt;span class="nx"&gt;animals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;animal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;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;animal&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;h3&gt;
  
  
  How it works
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;forEach()&lt;/code&gt; method loops over every element in the array.&lt;/li&gt;
&lt;li&gt;It picks each element in the array and logs it to the console.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Note:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;forEach()&lt;/code&gt; method does not mutate the array.&lt;/li&gt;
&lt;li&gt;The method returns &lt;code&gt;undefined&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope you have been able to learn something new about the &lt;code&gt;map()&lt;/code&gt;, &lt;code&gt;filter()&lt;/code&gt; and &lt;code&gt;forEach()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;If you have any questions, and or more insights into the topic, please drop a message for me in the comment section below. &lt;/p&gt;

&lt;h2&gt;
  
  
  Resources and further reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter"&gt;Array.prototype.filter()&lt;/a&gt;, MDN&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map"&gt;Array.prototype.map()&lt;/a&gt;, MDN&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach"&gt;Array.prototype.forEach()&lt;/a&gt;, MDN&lt;/li&gt;
&lt;/ul&gt;

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