Reduce array of objects on key and sum value into array
<p>I have the following object:</p>

<pre><code>data = [
  { name: 'foo', type: 'fizz', val: 9 },
  { name: 'foo', type: 'buzz', val: 3 },
  { name: 'bar', type: 'fizz', val: 4 },
  { name: 'bar', type: 'buzz', val: 7 },
];
</code></pre>

<p>And used lodash map:</p>

<pre><code>result = _.map(data, function item, idx){
  return {
    key: item[key],
    values: item.value,
  }
}
</code></pre>

<p>Which results in:</p>

<pre><code>[
  { key: foo, val: 9 },
  { key: foo, val: 3 },
  { key: bar, val: 4 },
  { key: bar, val: 7 },
]
</code></pre>

<p>but now I'm trying to return:</p>

<pre><code>[
  { key: 'foo', val: 12 },
  { key: 'bar', val: 11 },
]
</code></pre>

<p>I tried using reduce which seems to only output to a single object, which I could then convert back into an array, but I feel like there must be an elegant way to use lodash to go from my source data straight to my desired result without all of the intermediate steps.</p>

<p>I thought <a href="https://stackoverflow.com/questions/32659248/reduce-an-array-of-objects-and-sum-property-for-each-distinct-object">this</a> was addressing my exact problem, but it seems like quite a bit of work just to have to convert the object into the desired array of objects outlined above.</p>

<p>Cheers.</p>