{"componentChunkName":"component---src-templates-blog-post-js","path":"/discovery-the-4-difference-between-map-and-foreach/","result":{"data":{"markdownRemark":{"fields":{"slug":"/discovery-the-4-difference-between-map-and-foreach/"},"frontmatter":{"titulo":"Discovery the 4 difference between map and foreach","data":"30/01/2021","tag":"Javascript","image":{"childImageSharp":{"fluid":{"src":"/static/21a3d24ab7ce38466dc74d326ee91eec/46604/mapfor.png","base64":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABCklEQVQ4y61U22rCQBCdL1Ko4CUxBtFoGmKIJtZErUJrBcE3f0V86QeUPvQPA0d340ZT8QYTOGT2zOxhDrO7BOaPfn7/wAkqlgxwgoymA06QbtrgRE5Qa3Rxa33OXaulaxufRSaogmrdQkVvy3/N6MiCc07FFd06Io1VrRKVHQrSdkP0+jFee0N0nQBmy005P0LnsBaxNxjLvIDg3WNO1OYsVw+C4WiO1XqD+H0Bx3tD2/YxmS0RTxfZ5ul8iWjyiY+vteSD0UzmmpYnm8pZFhbKWitLCD61dbIs8qqurKX8/0HRo9O9N4yc5XtH5JbQxZTZb0rhpQ5O0Hb3DU4Q+3uYJAk4wd8ht+Aes8XJsuk6x0MAAAAASUVORK5CYII=","aspectRatio":1,"srcSet":"/static/21a3d24ab7ce38466dc74d326ee91eec/7e7e9/mapfor.png 225w,\n/static/21a3d24ab7ce38466dc74d326ee91eec/62b1f/mapfor.png 450w,\n/static/21a3d24ab7ce38466dc74d326ee91eec/46604/mapfor.png 500w","sizes":"(max-width: 500px) 100vw, 500px"}}}},"html":"<p>Today you will discovery the difference between <code class=\"language-text\">map()</code>, <code class=\"language-text\">foreach()</code> and how these teachings made me <strong>developer better</strong> in JavaScript.</p>\n<p>How do you know, JavaScript has something methods that help us work with arrays. The most famous are <code class=\"language-text\">seuArray.map()</code> and <code class=\"language-text\">seuArray.forEach()</code>, but that world is the bigger than you can imagine, today exists more than 10 methods that help us work with arrays 😮 (the next post I talk about these)</p>\n<p>But for those starting out in programming, have difficult to understand the concepts and difference of two. <strong>I'm here for that</strong>, help you to understand the difference between these methods 😃</p>\n<p>Topics</p>\n<ul>\n<li>Definition</li>\n<li>The return</li>\n<li>Trigger other methods</li>\n<li>Mutability</li>\n<li>Performance speed</li>\n<li>Conclusion </li>\n</ul>\n<h3>🏳️ Definition</h3>\n<h4>forEach()</h4>\n<blockquote>\n<p>The <code class=\"language-text\">forEach()</code> method performs a function provided once for each key / value pair in the Map object, in order of insertion. <a href=\"https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach\">ref</a></p>\n</blockquote>\n<p>By default, this method go through all the items in an array, like a <strong>normal loop</strong>.</p>\n<h4>map()</h4>\n<blockquote>\n<p>The <code class=\"language-text\">map()</code> invokes the callback function passed by argument for each element of the Array and returns a new Array as a result.<a href=\"https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/map\">ref</a></p>\n</blockquote>\n<p>The practice the method foreach and map look the same, but with the difference to <strong>return</strong>. The foreach manipulate the real data for from array and map create a new array.</p>\n<h3>📍 The return</h3>\n<p>How I talk about previously, the deffinition the method <code class=\"language-text\">forEach()</code> just walks over the array, while the <code class=\"language-text\">map()</code> return new value.</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> items <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span><span class=\"token number\">2</span><span class=\"token punctuation\">,</span><span class=\"token number\">3</span><span class=\"token punctuation\">,</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span>\n\n<span class=\"token keyword\">const</span> values <span class=\"token operator\">=</span> items<span class=\"token punctuation\">.</span><span class=\"token function\">forEach</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">item</span> <span class=\"token operator\">=></span> item <span class=\"token operator\">*</span> item<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">// return: undefined</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>values<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> items <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span><span class=\"token number\">2</span><span class=\"token punctuation\">,</span><span class=\"token number\">3</span><span class=\"token punctuation\">,</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span>\n\n<span class=\"token keyword\">const</span> values <span class=\"token operator\">=</span> items<span class=\"token punctuation\">.</span><span class=\"token function\">map</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">item</span> <span class=\"token operator\">=></span> item <span class=\"token operator\">*</span> item<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">// return: [1, 4, 9, 16]</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>values<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<h3>⏩ Trigger other methods</h3>\n<p>An important difference between these two methods is that in <code class=\"language-text\">map()</code> you can trigger other methods, such as <code class=\"language-text\">filter()</code>, <code class=\"language-text\">sort()</code>, <code class=\"language-text\">reduce()</code>. In <code class=\"language-text\">foreach()</code> you do not have access to these methods because it does not return a value.</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> items <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">,</span> <span class=\"token number\">4</span><span class=\"token punctuation\">]</span>\n\nitems<span class=\"token punctuation\">.</span><span class=\"token function\">forEach</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">item</span> <span class=\"token operator\">=></span> item <span class=\"token operator\">*</span> item<span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">reduce</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">total<span class=\"token punctuation\">,</span> value</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> total <span class=\"token operator\">+</span> value<span class=\"token punctuation\">)</span>\n<span class=\"token comment\">//return: Uncaught TypeError: Cannot read property 'reduce' of undefined</span>\n\nitems<span class=\"token punctuation\">.</span><span class=\"token function\">map</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">item</span> <span class=\"token operator\">=></span> item <span class=\"token operator\">*</span> item<span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">reduce</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">total<span class=\"token punctuation\">,</span> value</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> total <span class=\"token operator\">+</span> value<span class=\"token punctuation\">)</span>\n<span class=\"token comment\">//return: value: 30</span></code></pre></div>\n<h3>📌 Mutability</h3>\n<blockquote>\n<p>forEach() does not change the array in which it is called. (However, callback can do this).</p>\n<p>map() does not change the array it is called in (although callback, if called, can do that). <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript\">ref</a></p>\n</blockquote>\n<p><img src=\"https://media.giphy.com/media/3o7527pa7qs9kCG78A/giphy.gif\"></p>\n<p>My opinion that definition isn't totally clear, that is confusing. </p>\n<p>To find out what doesn't change the original array, the first ones have to check how these two methods work.</p>\n<p>The <code class=\"language-text\">map()</code> returns one in the array transformed with the same amount of data. In the case of <code class=\"language-text\">forEach()</code>, even if it returns <code class=\"language-text\">undefined</code>, it  <strong>will</strong>  change ** the original array with the callback.</p>\n<p>Therefore, we clearly see that <code class=\"language-text\">map()</code> depends on immutability and <code class=\"language-text\">forEach()</code> is a  <strong>modifying</strong>  method.</p>\n<h3>🚀 Performance speed</h3>\n<p>For you to validate the speed and performance it depends on several things of the computer, the amount of data, processing, among other things. You can check for yourself with this example below to see which is faster.</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> items <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">,</span> <span class=\"token number\">4</span><span class=\"token punctuation\">,</span> <span class=\"token number\">5</span><span class=\"token punctuation\">]</span>\n\n<span class=\"token keyword\">const</span> startForEach <span class=\"token operator\">=</span> performance<span class=\"token punctuation\">.</span><span class=\"token function\">now</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\nitems<span class=\"token punctuation\">.</span><span class=\"token function\">forEach</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">x</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">(</span>x <span class=\"token operator\">+</span> x<span class=\"token punctuation\">)</span> <span class=\"token operator\">*</span> <span class=\"token number\">10000000000</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token keyword\">const</span> endForEach <span class=\"token operator\">=</span> performance<span class=\"token punctuation\">.</span><span class=\"token function\">now</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">Speed [forEach]: </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>endForEach <span class=\"token operator\">-</span> startForEach<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\"> miliseconds</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span>\n\n<span class=\"token keyword\">const</span> startMap <span class=\"token operator\">=</span> performance<span class=\"token punctuation\">.</span><span class=\"token function\">now</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\nitems<span class=\"token punctuation\">.</span><span class=\"token function\">map</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">x</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">(</span>x <span class=\"token operator\">+</span> x<span class=\"token punctuation\">)</span> <span class=\"token operator\">*</span> <span class=\"token number\">10000000000</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token keyword\">const</span> endMap <span class=\"token operator\">=</span> performance<span class=\"token punctuation\">.</span><span class=\"token function\">now</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">Speed [map]: </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>endMap <span class=\"token operator\">-</span> startMap<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\"> miliseconds</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span>\n<span class=\"token comment\">//return: value: 30</span></code></pre></div>\n<h3>🤓 Conclusion</h3>\n<p>Which one should I use? Like everything in the world: It depends. If you plan to change or use the data, it is preferable to select <code class=\"language-text\">map()</code>, as it returns a new array with the transformed data. But, if you don't need the new array, use <code class=\"language-text\">forEach().</code></p>\n<p>I hope this short post has helped you in some way!</p>\n<blockquote>\n<p>Suggestions and criticisms are highly appreciated.</p>\n</blockquote>\n<p>If there are more differences, please share them in the comments, otherwise, thanks for getting here 😍.</p>\n<p><img src=\"https://media.giphy.com/media/l2JI1JzL6YS8z5KUM/giphy.gif\"></p>","timeToRead":3}},"pageContext":{"slug":"/discovery-the-4-difference-between-map-and-foreach/","previousPost":{"fields":{"slug":"/test-in-nodejs/"},"frontmatter":{"templateKey":"blog-post","data":"05 de Fevereiro de 2021","tag":"NodeJs","titulo":"It's time to learn how to test!","locale":"en"},"timeToRead":9},"nextPost":{"fields":{"slug":"/flexbox/"},"frontmatter":{"templateKey":"blog-post","data":"19 de Janeiro de 2021","tag":"Programming","titulo":" FlexBox - Visual Guide","locale":"en"},"timeToRead":1},"locale":"en"}}}