Skip to content

Instantly share code, notes, and snippets.

@lluisruscalleda
Last active March 5, 2020 15:16
Show Gist options
  • Save lluisruscalleda/5c035d13972a7a33b5e555018e0da763 to your computer and use it in GitHub Desktop.
Save lluisruscalleda/5c035d13972a7a33b5e555018e0da763 to your computer and use it in GitHub Desktop.
Sum values from an array of objects with the same keys in javascript
export const sumObjs = (...objs) => Object.assign({},
...objs.reduce((acc, obj) => Object.keys(obj).map((key) => {
const ret = {}; ret[key] = acc[key] + obj[key];
return ret;
})));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment