title: JavaScript merge objects with same key and sum their values
body: <p>The code is working fine as expected but the <code>name</code> parameter is missing in response.</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>var objArr= [{'location_id':1,'quantity':20,'name':'AB'},{'location_id':1,'quantity':20,'name':'AB'},{'location_id':3,'quantity':20,'name':'cd'}]
  
  // first, convert data into a Map with reduce
  let counts = objArr.reduce((prev, curr) =&gt; {
    let count = prev.get(curr.location_id) || 0;
    prev.set(curr.location_id, (curr.quantity + count),curr.name);
    return prev;
  }, new Map());
  
  // then, map your counts object back to an array
  let reducedObjArr = [...counts].map(([location_id, quantity,name]) =&gt; {
    return {location_id, quantity,name}
  })
  
  
  console.log (reducedObjArr);</code></pre>
  </div>
  </div>
  </p>
  <p><strong>Expected Response:</strong></p>
  <pre><code>[
  {&quot;location_id&quot;:1,&quot;quantity&quot;:40, &quot;name&quot;:'AB'},
  {&quot;location_id&quot;:3,&quot;quantity&quot;:20, &quot;name&quot;:'cd'}
  ]
  </code></pre>