var 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 } ]; var result = _(data) .map('name') .uniq() .map(key => ({ key, val: _(data).filter({ name: key }).sumBy('val') })) .value(); console.log(result);
Search thousands of free JavaScript snippets that you can quickly copy and paste into your web pages. Get free JavaScript tutorials, references, code, menus, calendars, popup windows, games, and much more.
Reduce array of objects on key and sum value into array
You can get all the unique names using map() and uniq(), and then map() each name to get their respective sums using sumBy().
Subscribe to:
Post Comments (Atom)
What is Security Engineering? Part 2.
A broad overview of the ever-developing security engineering field; a domain that can feel intimidating to some software engineers. With Nie...
No comments:
Post a Comment