title: Nodejs: Object aggregation
body: <p>I am using Nodejs &amp; would like to group my object array based on few attributes.
  example <code>attr_id</code> &amp; <code>type</code> will be unique</p>
  <pre><code>const normalizedList =[ {
          &quot;attr_id&quot;: 1,
          &quot;type&quot;: &quot;color&quot;,
          &quot;value_index&quot;: 10,
          &quot;value_label&quot;: &quot;Blue&quot;
      },
      {
          &quot;attr_id&quot;: 1,
          &quot;type&quot;: &quot;color&quot;,
          &quot;value_index&quot;: 15,
          &quot;value_label&quot;: &quot;Red&quot;
      },
      {
          &quot;attr_id&quot;: 2,
          &quot;type&quot;: &quot;size&quot;,
          &quot;value_index&quot;: 10,
          &quot;value_label&quot;: &quot;Small&quot;
      },
      {
          &quot;attr_id&quot;: 2,
          &quot;type&quot;: &quot;size&quot;,
          &quot;value_index&quot;: 14,
          &quot;value_label&quot;: &quot;Big&quot;
      }
  ];
  </code></pre>
  <p>Needs to be converted to</p>
  <pre><code>[{
          &quot;attr_id&quot;: 1,
          &quot;type&quot;: &quot;color&quot;,
          &quot;values&quot;: [{
                  &quot;index&quot;: 10,
                  &quot;label&quot;: &quot;Blue&quot;
              }, {
                  &quot;index&quot;: 15,
                  &quot;label&quot;: &quot;Red&quot;
              }
          ]
      }, {
          &quot;attr_id&quot;: 2,
          &quot;type&quot;: &quot;size&quot;,
          &quot;values&quot;: [{
                  &quot;index&quot;: 10,
                  &quot;label&quot;: &quot;Small&quot;
              }, {
                  &quot;index&quot;: 14,
                  &quot;label&quot;: &quot;Big&quot;
              }
          ]
      }
  ]
  </code></pre>
  <p>I was trying to do this without any packages like &quot;underscore&quot; or &quot;json-aggregate&quot;. as not many places in the code we do any grouping/aggregation</p>
  <p>Below is how I was able to solve</p>
  <pre><code>const groupJson = (arr,g_label) =&gt;{
  // group the list by 'attr_id'
      const groupedAttrList = normalizedList.reduce((acc, obj)=&gt;{
        let key = obj[g_label]
        if (!acc[key]) {
          acc[key] = []
        }
        acc[key].push(obj)
        return acc
      },{});
      const finalArray = [];
  
    // Now iterate over the groupedAttrList 
      for (let typeRow in groupedAttrList){
        let resultObj = {};
        let tempRow = groupedAttrList[typeRow];
        // as attr_id,type are unique for the set; picking it from 1st
        const {attr_id,type} = tempRow [0];
        resultObj .attr_id= attr_id;
        resultObj .type= type;
        // get all the values
        resultObj .values = tempRow .map(v=&gt;{
          return {index:v.value_index,label:v.value_label};
        });
        finalArray.push(resultObj );
      }
      console.log(finalArray);
  };
  </code></pre>
  <p>Test</p>
  <pre><code>let tempResult = groupJson(normalizedList,'attr_id');
  </code></pre>
  <p>wanted to learn if there are better ways to do it</p>