30

How can I sum the values in objects which share a common key? I need to use Lodash for this because I need good performance if these arrays get huge.

var prjMgrValues = [
   {"proj_mgr":"Jack ProjManager","submitted_dollars":12000},
   {"proj_mgr":"Jack ProjManager","submitted_dollars":750000},
   {"proj_mgr":"Joe ProjManager","submitted_dollars":45000}
]

I'm looking for an output of

[
  {"proj_mgr":"Jack ProjManager","submitted_dollars":762000},
  {"proj_mgr":"Joe ProjManager","submitted_dollars":45000}
]
4castle
  • 32,613
  • 11
  • 69
  • 106
S. Hussey
  • 377
  • 1
  • 3
  • 11
  • 3
    So a library adding helper functions around builtin tools should be faster than the builtin tools? Just a small [test case](https://jsfiddle.net/727reyn9/). Spoiler: lodash is magnitudes slower – Andreas Aug 04 '16 at 19:05
  • 2
    @Andreas True. From the OP's perspective they may not have known how to use the builtin tools efficiently, so they trusted that lodash would be better than something they wrote themselves. Your "builtin" code should be an answer. – 4castle Aug 04 '16 at 19:19
  • @4castle If the question wasn't explicitly about lodash this should have been marked as duplicate as there are already hundreds of questions à la "group array of objects by key" out there – Andreas Aug 04 '16 at 19:22
  • @Andreas The grouping is only half of the strategy. Would it be appropriate to mark as a duplicate when it's not clear how it relates to the question? I'm not confident in how to handle answers which cherry-pick algorithms from other places. – 4castle Aug 04 '16 at 19:24
  • @4castle I think not. At least for me the duplicate should clearly show the solution to the problem. You should head over to meta.stackoverflow.com and have a look at the different "duplicate" related topics – Andreas Aug 04 '16 at 19:31
  • I assumed lodash was better than builtin - I'm not a JavaScript developer and this project is MVP, I certainly search around but unlimited my search to lodash. At some point this whole application will be rewritten by a true development team. I am grateful to @4castle for answering - as much as I searched I didn't apparently use the right terms because his posts didn't show up. Noob whatever .. He helped and this worked close it as dup or not I got value. – S. Hussey Aug 05 '16 at 23:10

1 Answers1

77

This is a case of reduction for each unique element.

I always use _.groupBy and then _.map the result to an array after applying the reduction. In this case the reduction operation is _.sumBy.

var prjMgrValues = [
   {"proj_mgr":"Jack ProjManager","submitted_dollars":12000},
   {"proj_mgr":"Jack ProjManager","submitted_dollars":750000},
   {"proj_mgr":"Joe ProjManager","submitted_dollars":45000}
];

var output =
  _(prjMgrValues)
    .groupBy('proj_mgr')
    .map((objs, key) => ({
        'proj_mgr': key,
        'submitted_dollars': _.sumBy(objs, 'submitted_dollars') }))
    .value();

console.log(output);
<script src="https://cdn.jsdelivr.net/lodash/4.14.1/lodash.min.js"></script>
Chibuzo
  • 6,112
  • 3
  • 29
  • 51
4castle
  • 32,613
  • 11
  • 69
  • 106
  • 2
    This kind of operation is really common (hence why I answered). The last time I used it was [here](http://stackoverflow.com/q/38485493/5743988), and before that I used it [here](http://stackoverflow.com/q/38438645/5743988). So it's a good formula to keep in mind. – 4castle Aug 04 '16 at 18:44