<rss version="2.0">
  <channel>
    <title>Code on Evan Lovely</title>
    <link>https://www.evanlovely.com/categories/code/</link>
    <description></description>
    
    <language>en</language>
    
    <lastBuildDate>Thu, 09 May 2024 09:15:56 -0700</lastBuildDate>
    
    <item>
      <title>A case study of Client Side Rendering</title>
      <link>https://www.evanlovely.com/2024/05/09/a-case-study.html</link>
      <pubDate>Thu, 09 May 2024 09:15:56 -0700</pubDate>
      
      <guid>http://evanlovely.micro.blog/2024/05/09/a-case-study.html</guid>
      <description>&lt;p&gt;Wow, &lt;a href=&#34;https://github.com/theninthsky/client-side-rendering&#34;&gt;this&lt;/a&gt; has to be one of the best write ups of how to optimize your Webpack config that I’ve seen. Great dive into React page routing optimization as well.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>When to reach for different JS methods while manipulating data</title>
      <link>https://www.evanlovely.com/2023/01/26/when-to-reach.html</link>
      <pubDate>Thu, 26 Jan 2023 22:55:26 -0700</pubDate>
      
      <guid>http://evanlovely.micro.blog/2023/01/26/when-to-reach.html</guid>
      <description>&lt;h1 id=&#34;array---array-structure-different-length-same&#34;&gt;Array -&amp;gt; Array; structure different, length same&lt;/h1&gt;
&lt;p&gt;Reach for &lt;code&gt;.map&lt;/code&gt;&lt;/p&gt;
&lt;h1 id=&#34;array---array-structure-same-length-different&#34;&gt;Array -&amp;gt; Array; structure same, length different&lt;/h1&gt;
&lt;p&gt;Reach for &lt;code&gt;.filter&lt;/code&gt;&lt;/p&gt;
&lt;h1 id=&#34;array---non-array-like-object&#34;&gt;Array -&amp;gt; Non-Array (like Object)&lt;/h1&gt;
&lt;p&gt;Reach for &lt;code&gt;.reduce&lt;/code&gt; - easily one of the most powerful and flexible Array methods.&lt;/p&gt;
&lt;h1 id=&#34;object---object-structure-different-length-same&#34;&gt;Object -&amp;gt; Object; structure different, length same&lt;/h1&gt;
&lt;p&gt;The &amp;ldquo;entries&amp;rdquo; are super helpful, given an object like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-js&#34; data-lang=&#34;js&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;const&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;data&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; {
  &lt;span style=&#34;color:#a6e22e&#34;&gt;firstName&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Evan&amp;#39;&lt;/span&gt;,
  &lt;span style=&#34;color:#a6e22e&#34;&gt;lastName&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Lovely&amp;#39;&lt;/span&gt;,
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &amp;ldquo;entries&amp;rdquo; (what you get if ran &lt;code&gt;Object.entries(data)&lt;/code&gt;) would be:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-js&#34; data-lang=&#34;js&#34;&gt;[
  [&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;firstName&amp;#39;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Evan&amp;#39;&lt;/span&gt;],
  [&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;lastName&amp;#39;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Lovely&amp;#39;&lt;/span&gt;],
]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Being able to go to with &lt;code&gt;Object.entries&lt;/code&gt; and from with &lt;code&gt;Object.fromEntries&lt;/code&gt; is very powerful and you then can use all the Array methods to do manipulations - so in the case of structure different, length same you&amp;rsquo;d reach for &lt;code&gt;.map&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-js&#34; data-lang=&#34;js&#34;&gt;Object.&lt;span style=&#34;color:#a6e22e&#34;&gt;fromEntries&lt;/span&gt;(
  Object.&lt;span style=&#34;color:#a6e22e&#34;&gt;entries&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;myObject&lt;/span&gt;).&lt;span style=&#34;color:#a6e22e&#34;&gt;map&lt;/span&gt;(([&lt;span style=&#34;color:#a6e22e&#34;&gt;key&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;value&lt;/span&gt;]) =&amp;gt; {
    &lt;span style=&#34;color:#75715e&#34;&gt;// manipulate
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; [&lt;span style=&#34;color:#a6e22e&#34;&gt;key&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;value&lt;/span&gt;];
  }),
);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h1 id=&#34;array---array-different-structure-different-length&#34;&gt;Array -&amp;gt; Array; different structure, different length&lt;/h1&gt;
&lt;p&gt;It&amp;rsquo;s common to chain &lt;code&gt;.filter&lt;/code&gt; and &lt;code&gt;.map&lt;/code&gt; together which works great if you are wanting a smaller number of items than you started with, but if you want to have the list grow - you&amp;rsquo;ll need to reach for something more powerful: &lt;code&gt;.flatMap&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The common use of &lt;code&gt;.flatMap&lt;/code&gt; is to use &lt;code&gt;.map&lt;/code&gt; and then if you end up with an array of arrays to &lt;code&gt;.flat&lt;/code&gt; them - however it can be much more powerful than that, from the MDN docs page:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;For adding and removing items during a map()&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;flatMap&lt;/code&gt; can be used as a way to add and remove items (modify the number of items) during a &lt;code&gt;map&lt;/code&gt;. In other words, it allows you to map many items to many items (by handling each input item separately), rather than always one-to-one. In this sense, it works like the opposite of &lt;code&gt;filter&lt;/code&gt;. &lt;em&gt;Return a 1-element array to keep the item, a multiple-element array to add items, or a 0-element array to remove the item.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
</description>
    </item>
    
  </channel>
</rss>