<?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: fatemeh zamanipour</title>
    <description>The latest articles on DEV Community by fatemeh zamanipour (@eldevflo).</description>
    <link>https://dev.to/eldevflo</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%2F1056201%2F95c4a62d-a768-4022-8f5d-c5c349278a0d.jpg</url>
      <title>DEV Community: fatemeh zamanipour</title>
      <link>https://dev.to/eldevflo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eldevflo"/>
    <language>en</language>
    <item>
      <title>Ways to group data of an array.</title>
      <dc:creator>fatemeh zamanipour</dc:creator>
      <pubDate>Tue, 18 Jul 2023 15:28:17 +0000</pubDate>
      <link>https://dev.to/eldevflo/ways-to-group-data-of-an-array-56g8</link>
      <guid>https://dev.to/eldevflo/ways-to-group-data-of-an-array-56g8</guid>
      <description>&lt;p&gt;Let's say you have an array of colors and you aim to categorize the colors into tow groups of dark colors and light colors. &lt;br&gt;
how would you do this in JavaScript?&lt;br&gt;
well...there are different ways to group the data and in this article we'll go through some of them(trust me, the best ones😉).&lt;/p&gt;

&lt;p&gt;Today, we will cover three ways of grouping the data:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;using &lt;code&gt;Map()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;using &lt;code&gt;array.reduce()&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;using &lt;code&gt;array.groupBy()&lt;/code&gt; and &lt;code&gt;array.groupByToMap()&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;so... let's get started&lt;/p&gt;
&lt;h3&gt;
  
  
  1. using &lt;code&gt;Map()&lt;/code&gt;:
&lt;/h3&gt;

&lt;p&gt;Honestly, Map() is my favorite One. &lt;/p&gt;

&lt;p&gt;it's simple and we can get the values easily using the &lt;code&gt;get()&lt;/code&gt; method and set them using &lt;code&gt;set()&lt;/code&gt; method. it's more performant and compatible.&lt;br&gt;
anyways... this is how you can group the colors using &lt;code&gt;Map()&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const colors= [
    {
        name: "orange",
        type: 'light',
    },
     {
        name: "black",
        type: 'dark,
    },
     {
        name: "red",
         type: 'light',
    },
     {
        name: "blue",
       type: 'dark,
    },
     {
        name: "brown",
        type: 'dark,
    },
     {
        name: "yellow",
        type: 'light',
    },
]
const colorsMap = new Map()
colors.forEach(color =&amp;gt;colorsMap.set(color.type, color))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. using &lt;code&gt;array.reduce()&lt;/code&gt; :
&lt;/h3&gt;

&lt;p&gt;Last but not least we can take advantage of &lt;code&gt;array.reduce()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const groupByDarkness = colors.reduce((group, color) =&amp;gt; {
  const { type } = color;
  group[type] = group[key ] ?? [];
  group[type].push(color);
  return group;
}, {});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. using &lt;code&gt;array.groupBy()&lt;/code&gt; and &lt;code&gt;array.groupByToMap()&lt;/code&gt;:
&lt;/h3&gt;

&lt;p&gt;this method has just introduced by &lt;strong&gt;ES&lt;/strong&gt;.&lt;br&gt;
Moreover, since the group and groupToMap array methods are still in the proposal stage, we can’t use them in our JavaScript projects. However, if we use polyfill, browsers will understand what those methods do and run them for us without throwing an error whenever we use them.&lt;/p&gt;

&lt;p&gt;Simply put, a polyfill is a code that implements a feature on web browsers that do not natively support the feature. The core-js is a robust polyfill that we can use to make the group and groupToMap available in the browser.&lt;/p&gt;

&lt;p&gt;To get started with core-js, we can run the following code in our project to install the library:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save core-js@3.26.1

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's how to use array.groupBy() to create a color group:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const groupByDarkness = colors.groupBy(color =&amp;gt; {
  return color.type 
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this returns an object where properties are light/dark and values are arrays of colors.&lt;/p&gt;

&lt;p&gt;Sometimes you may want to use a Map instead of a plain object. The benefit of Map is that it accepts any data type as a key, but the plain object is limited to strings and symbols only.&lt;/p&gt;

&lt;p&gt;So, if you'd like to group data into a &lt;code&gt;Map&lt;/code&gt;, you can use the method &lt;code&gt;array.groupByToMap()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;array.groupByToMap(callback)&lt;/code&gt; works the same way as &lt;code&gt;array.groupBy(callback)&lt;/code&gt;, only that it groups items into a Map instead of a plain JavaScript object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const groupByDarkness = colors.groupByToMap(color =&amp;gt; {
  return color.type 
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;using each of the above methods you'll get the following result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "light": [
        {
            "name": "orange",
             "type": "light"
        },
        {
            "name": "red",
            "type": "light"
        },
        {
            "name": "yellow",
             "type": "light"
        }
    ],
    "dark": [
        {
            "name": "black",
             "type": "dark"
        },
        {
            "name": "blue",
            "type": "dark"
        },
        {
            "name": "brown",
            "type": "dark"
        }
    ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>frontend</category>
      <category>coding</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
