How to group by, calculate sum and get average in JavaScript array?

I have array of object Expected result: I use reduce() method to group the objects in an array by ‘group’ and calulate sum: How to group object of Array by key ‘group’ and calulate average?. I tried: But Expected result: Answer You could simply use reduce to get the total of age groups. And use object.keys length to get the average of your total as new object from getAvg function. Demo: