<?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: Carl Espiritu</title>
    <description>The latest articles on DEV Community by Carl Espiritu (@espi0054).</description>
    <link>https://dev.to/espi0054</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%2F722675%2F255b2f3f-5564-4a32-a567-a13676f636c4.png</url>
      <title>DEV Community: Carl Espiritu</title>
      <link>https://dev.to/espi0054</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/espi0054"/>
    <language>en</language>
    <item>
      <title>array.prototype.map()</title>
      <dc:creator>Carl Espiritu</dc:creator>
      <pubDate>Sun, 17 Oct 2021 03:50:42 +0000</pubDate>
      <link>https://dev.to/espi0054/arrayprototypemap-2b70</link>
      <guid>https://dev.to/espi0054/arrayprototypemap-2b70</guid>
      <description>&lt;p&gt;In this tutorial, we will be exploring how the method &lt;code&gt;map()&lt;/code&gt; works and how to use it effectively.&lt;/p&gt;

&lt;p&gt;Method &lt;code&gt;map()&lt;/code&gt; that only works on arrays that goes through an array and iterates all the data inside and creates a new array along with a function applied to the elements of the array, leaving the original array untouched.&lt;/p&gt;

&lt;h3&gt;
  
  
  The syntax is as follows:
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;array.map(function(element, index, array), thisValue)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;the method works by creating a callback function and every time it is called, it goes through each &lt;code&gt;element&lt;/code&gt;, &lt;code&gt;index&lt;/code&gt; or &lt;code&gt;array&lt;/code&gt; and returns the result as a new array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parameters&lt;/strong&gt;&lt;br&gt;
*element: This parameter holds the value of the elements.&lt;br&gt;
*index(optional): This parameter holds the current index of the current value of the array.&lt;br&gt;
*array: The original array that was created.&lt;br&gt;
*thisArg(optional): This function is used when we execute the callback function.&lt;/p&gt;

&lt;h3&gt;
  
  
  Map in action
&lt;/h3&gt;

&lt;p&gt;First things first, we need to create an array, to do so, we need to declare a name of the array and in this case we are going to call our array &lt;code&gt;myToys&lt;/code&gt;.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JpSxEnMB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q0sv64tpgy3p53eegxgv.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JpSxEnMB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q0sv64tpgy3p53eegxgv.PNG" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we are ready for the next step which is using &lt;code&gt;map()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In this example, we are going to convert all the letters from our &lt;code&gt;myToys&lt;/code&gt; array into uppercase letters and use the &lt;code&gt;thisArg&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;To use &lt;code&gt;map()&lt;/code&gt; we will need to create a new variable for the array, so for our new variable we will call it &lt;code&gt;myToys2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then we will create a name for our callback function so for that we will name it &lt;code&gt;convertToys&lt;/code&gt;.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SX-nM0eH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sx4ydlf7oinf9tncd7wk.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SX-nM0eH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sx4ydlf7oinf9tncd7wk.PNG" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Then we will use &lt;code&gt;this.&lt;/code&gt; function in order to convert them all to uppercase.&lt;/p&gt;

&lt;p&gt;The result should look like this.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gmFbkPYC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aacwvkv1yofm5kdoe9cx.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gmFbkPYC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aacwvkv1yofm5kdoe9cx.PNG" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Arrow function
&lt;/h3&gt;

&lt;p&gt;We can also use the &lt;code&gt;arrow&lt;/code&gt; function to further shorten our code which makes it less cluttered in your workspace.&lt;/p&gt;

&lt;p&gt;In this example, we are going to create a new array that contains the number of letters in each toy.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ADj23wI2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3zh2ur64u1c60tbe58gj.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ADj23wI2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3zh2ur64u1c60tbe58gj.PNG" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;See how short that syntax is?&lt;br&gt;
This is what it looks like if we print it out&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h2ysjcm3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/arsyniyig8hdhxhzhbr2.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h2ysjcm3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/arsyniyig8hdhxhzhbr2.PNG" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To wrap things up, the &lt;code&gt;map()&lt;/code&gt; method has a lot of uses, and it is mostly used to clean up your workspace. Sure there are other methods that gets you the same result such as the &lt;code&gt;for()&lt;/code&gt; loop but the syntax is more complicated than the arrow function that we used; it is clean and fast&lt;/p&gt;

&lt;p&gt;Also the benefits of using &lt;code&gt;map()&lt;/code&gt; is that it does not harm the original array as it creates a new one leaving the original one untouched and can be used as a reference.&lt;/p&gt;

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