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)
The 6 New Rules of Communicating
The era of teleprompters and talking points has come to an end ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ...
-
code.gs // 1. Enter sheet name where data is to be written below var SHEET_NAME = "Sheet1" ; // 2. Run > setup // // 3....
No comments:
Post a Comment