<?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: falbech</title>
    <description>The latest articles on DEV Community by falbech (@falbech).</description>
    <link>https://dev.to/falbech</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F454857%2F364d3f4e-8ec4-4252-8470-73a5c4c1f454.jpeg</url>
      <title>DEV Community: falbech</title>
      <link>https://dev.to/falbech</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/falbech"/>
    <language>en</language>
    <item>
      <title>JS Rewrited #01 - Array.map()</title>
      <dc:creator>falbech</dc:creator>
      <pubDate>Wed, 06 Jan 2021 16:33:41 +0000</pubDate>
      <link>https://dev.to/falbech/js-rewrited-01-array-map-3o6h</link>
      <guid>https://dev.to/falbech/js-rewrited-01-array-map-3o6h</guid>
      <description>&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this article, we will discuss what the &lt;em&gt;Array.map&lt;/em&gt; function is, its usage, and recreate its behavior by creating a brand new custom function to get a deeper understanding of how it works in detail.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;em&gt;map&lt;/em&gt; function&lt;/li&gt;
&lt;li&gt;Usage&lt;/li&gt;
&lt;li&gt;Function rewriting&lt;/li&gt;
&lt;li&gt;Resources&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  1. The &lt;em&gt;map&lt;/em&gt; function
&lt;/h2&gt;

&lt;p&gt;Array's map function is nothing more than a functional way to transform each one of the elements of an array into something else.&lt;/p&gt;

&lt;p&gt;Basically, this function takes as input a &lt;em&gt;callback&lt;/em&gt; function provided by the developer and an optional &lt;em&gt;thisArg&lt;/em&gt; (value to be used as &lt;em&gt;this&lt;/em&gt; by the callback). It has the following signature:&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="nb"&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;callbackFn&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="nx"&gt;currentIndex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;currentArray&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;thisArg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It accepts two inputs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;callbackFn&lt;/em&gt;: a function provided by the developer to perform any operation in the element of the array. It *&lt;em&gt;must return *&lt;/em&gt; the new intended value which the element is being mapped into. &lt;br&gt;
This function can receive three arguments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;currentElement&lt;/em&gt; : the current element being iterated.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;currentIndex&lt;/em&gt; : the current index of the element being iterated.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;currentArray&lt;/em&gt; : the current array object.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;thisArg&lt;/em&gt; : the value used as &lt;em&gt;this&lt;/em&gt; by the provided &lt;em&gt;callbackFn&lt;/em&gt;. This is an optional argument.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The returned value of this function is a brand new array containing all the mapped elements.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Usage
&lt;/h2&gt;

&lt;p&gt;In this simple example, a function to square a number was defined to be used as the &lt;em&gt;map's&lt;/em&gt; callback. Given an array of numbers, each one of the elements will be mapped to its squared value in a brand new array as the result of the map operation.&lt;/p&gt;

&lt;p&gt;For the sake of simplicity, the &lt;em&gt;thisArg&lt;/em&gt; was omitted.&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="cm"&gt;/* Defines a squaring function to be used as a callback argument */&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;squareFn&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="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;currentArray&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;currentElement&lt;/span&gt;&lt;span class="o"&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="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="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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;squaredNumbers&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="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;squareFn&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="cm"&gt;/* Prints the brand new generated list */&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;squaredNumbers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="cm"&gt;/* output: [1, 4, 9, 16, 25] */&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Interesting gotcha:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you wish to use the optional parameter &lt;em&gt;thisArg&lt;/em&gt;, be sure your callback function definition is done in the standard way (&lt;code&gt;function myFunc(){}&lt;/code&gt;), not with an arrow function  expression (&lt;code&gt;const myFunc = () =&amp;gt; {}&lt;/code&gt;). This is required for the binding of the custom &lt;em&gt;this&lt;/em&gt; value to work properly inside the provided callback since the arrow function binds its context to its lexical scope.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Function rewriting
&lt;/h2&gt;

&lt;p&gt;Have you ever wondered what kind of magic provides the argument's values to the callback function?&lt;/p&gt;

&lt;p&gt;Knowing what the map function does, it is possible to recreate it with a new function that replicates its behavior to get a better understanding of how it works under the hood. This can be done by adding a new custom function to the javascript &lt;em&gt;Array&lt;/em&gt; object with the following implementation:&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="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;customMap&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;callback&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;thisArg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* Sets the "this" context to be used within the callback function, defaults to the array which this function was called. */&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;_this&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;thisArg&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="k"&gt;this&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;mappedArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
    &lt;span class="cm"&gt;/* For each element, the callback function is called passing the provided context for "this" keyword */&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;i&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;mappedArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&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;mappedArr&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;You can test the new &lt;em&gt;customMap&lt;/em&gt; function by replacing the &lt;em&gt;map&lt;/em&gt; call that was done previously. The same output should be displayed.&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;squaredNumbers&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="nx"&gt;customMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;squareFn&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PS.: This implementation might differ from the native one implemented by browsers.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Resources
&lt;/h2&gt;

&lt;p&gt;For further reference: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map"&gt;MDN - Array.map&lt;/a&gt; &lt;/li&gt;
&lt;li&gt; &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Callback_function"&gt;MDN - Callback function&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Conclusion
&lt;/h2&gt;

&lt;p&gt;This article aimed to give a deeper understanding of how the array's &lt;em&gt;map&lt;/em&gt; function works under the hood. The "magic" under the map function was demystified by recreating its implementation according to the function's original behavior.&lt;/p&gt;





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