I have an array of objects that I am looking to group by a given property and then aggregate through summing the properties. Here is an example of my data:
This is what I have:
const teams = [{posteam: 'ARI', week: 1, touchdowns: 2, fieldgoals: 3}, {posteam: 'ARI', week: 2, touchdowns: 4, fieldgoals: 1}, {posteam: 'ARI', week: 3, touchdowns: 1, fieldgoals: 4},{posteam: 'ATL', week: 1, touchdowns: 0, fieldgoals: 2}]
This is what I'm looking to make:
[{posteam: 'ARI', touchdowns: 7, fieldgoals: 8}, {posteam: 'ATL', touchdowns: 0, fieldgoals: 2}]
The "week" value isn't needed because it's only a label, not an actual measure. Otherwise, I'm looking to basically reduce the arrays' objects into one object per posteam
. I've looked at the reduce
and groupby
functions, but neither seems to be the best solution for doing what Python's groupby
and agg
functions do. Ideally, I'd like to make this work in instances where there's only one object with a given posteam
value.