title: How to group an array of objects by key name?
body: <p>I have an array of objects and I want to group them by object's key names. I have tried to do this with <code>Array.reduce()</code>, but the result is not good since it's grouped by values. Here is the example of an array I have and example of what I've tried so far</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const rawData = [
      {
      "EmailCampaignEvents.campaignRef": "my-first-campaign",
      "EmailCampaignEvents.customerRef": "Customer/0",
      "EmailCampaignEvents.emailRef": "Customer/0",
      "EmailCampaignEvents.eventType": "initial_open",
      "EmailCampaignEvents.occurredAt": "2021-02-25T14:03:04.000"
    },
    {
      "EmailCampaignEvents.campaignRef": "my-second-campaign",
      "EmailCampaignEvents.customerRef": "Customer/123",
      "EmailCampaignEvents.emailRef": "Customer/123",
      "EmailCampaignEvents.eventType": "delivery",
      "EmailCampaignEvents.occurredAt": "2021-03-25T14:03:04.000"
    },
    {
      "Events.occurredAt": "2020-11-11T15:44:07.000",
      "Events.relatedEntityRefFlat": "Customer/6711684",
      "Events.type": "as.commerce.transaction.completed"
    }
  ];
  
  result = rawData.reduce(function (r, a) {
          r[a["EmailCampaignEvents.eventType"]] = r[a["EmailCampaignEvents.eventType"]] || [];
          r[a["EmailCampaignEvents.eventType"]].push(a);
          r[a["Events.type"]] = r[a["Events.type"]] || [];
          r[a["Events.type"]].push(a);
          return r;
      }, Object.create(null));
  
  console.log(result);</code></pre>
  </div>
  </div>
  </p>
  <p>And the result I try to achieve is something like this</p>
  <pre><code>{ 
    &quot;EmailCampaignEvents.eventType&quot;: [
      {
        &quot;EmailCampaignEvents.campaignRef&quot;: &quot;my-first-campaign&quot;,
        &quot;EmailCampaignEvents.customerRef&quot;: &quot;Customer/0&quot;,
        &quot;EmailCampaignEvents.emailRef&quot;: &quot;Customer/0&quot;,
        &quot;EmailCampaignEvents.eventType&quot;: &quot;initial_open&quot;,
        &quot;EmailCampaignEvents.occurredAt&quot;: &quot;2021-02-25T14:03:04.000&quot;
      },
      {
        &quot;EmailCampaignEvents.campaignRef&quot;: &quot;my-second-campaign&quot;,
        &quot;EmailCampaignEvents.customerRef&quot;: &quot;Customer/123&quot;,
        &quot;EmailCampaignEvents.emailRef&quot;: &quot;Customer/123&quot;,
        &quot;EmailCampaignEvents.eventType&quot;: &quot;delivery&quot;,
        &quot;EmailCampaignEvents.occurredAt&quot;: &quot;2021-03-25T14:03:04.000&quot;
      },
    ],
    &quot;Events.type&quot;: [
      {
        &quot;Events.occurredAt&quot;: &quot;2020-11-11T15:44:07.000&quot;,
        &quot;Events.relatedEntityRefFlat&quot;: &quot;Customer/6711684&quot;,
        &quot;Events.type&quot;: &quot;as.commerce.transaction.completed&quot;
      }
    ]
  }
  </code></pre>
  <p>Does anyone know how can I achieve this, but not using lodash or any other library? Any example will be appreciated!</p>