<?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: John McElreavey</title>
    <description>The latest articles on DEV Community by John McElreavey (@jmcelreavey).</description>
    <link>https://dev.to/jmcelreavey</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%2F321000%2Ffb02eb2e-c788-4be8-a715-820bf38b74ab.jpeg</url>
      <title>DEV Community: John McElreavey</title>
      <link>https://dev.to/jmcelreavey</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jmcelreavey"/>
    <language>en</language>
    <item>
      <title>Good Map Usage?</title>
      <dc:creator>John McElreavey</dc:creator>
      <pubDate>Wed, 13 Oct 2021 15:09:39 +0000</pubDate>
      <link>https://dev.to/jmcelreavey/good-map-usage-32na</link>
      <guid>https://dev.to/jmcelreavey/good-map-usage-32na</guid>
      <description>&lt;p&gt;Do you think this is a good usage of map in terms on readability / performance?&lt;/p&gt;

&lt;p&gt;Lets say I'm making a list of notes but only allow 1 per day, You have a list of your notes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
{id: 1, date: "2021-10-12", content: "hello world!"}, 
{id: 2, date: "2021-10-13", content: "hope you have a great day!"}
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have a text field and a submit button, on click we call &lt;code&gt;upsertNote()&lt;/code&gt; which will update the array if it's the same day or will add a new record i.e:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;usertNote({date: "2021-10-13", content: "how you get through today!"})
// output:
{id: 2, date: "2021-10-13", content: "how you get through today!"}

usertNote({date: "2021-10-14", content: "Future me!"})
// output:
{id: 3, date: "2021-10-14", content: "future me!"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how one updates the existing entry and the other creates a new one. &lt;/p&gt;

&lt;p&gt;Now lets say we want to add to our existing list. We could do a search on the array for the id but what if we used a Map instead? &lt;/p&gt;

&lt;p&gt;Is that better in terms on readability and performace?&lt;/p&gt;

&lt;p&gt;e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const toDoList = new Map(originalArray.map((i) =&amp;gt; [i.id, i]))
const upsertToDoEntry = usertNote({date: "2021-10-14", content: "Future me!"})

toDoList.set(upsertToDoEntry.id, upsertToDoEntry)

// In our react if we wanted to map these out we'd have to do something like this:
Array.from(toDoList , ([id, toDoEntry]) =&amp;gt; (
&amp;lt;span key={id}&amp;gt;{toDoEntry.content}&amp;lt;/span&amp;gt;
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Would you implement it this way? :) &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>react</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
