Phrasing for the specific data manipulation I am working on is difficult, so pardon for the poor title - I will bounce back with a good example. I have the following javascript array of objects, containing some sports data:
[
{ team: "Knicks", assists: 24 },
{ team: "Knicks", assists: 12 },
{ team: "Knicks", assists: 17 },
{ team: "Knicks", assists: 19 },
{ team: "Warriors", assists: 31 },
{ team: "Warriors", assists: 25 },
{ team: "Warriors", assists: 20 },
{ team: "Spurs", assists: 15 },
{ team: "Spurs", assists: 17 },
{ team: "Spurs", assists: 32 },
{ team: "Spurs", assists: 12 },
{ team: "Spurs", assists: 18 }
]
and would like to tidy it up so that the data looks like this:
[
{ team: "Knicks", assists: [24, 36, 53, 72] },
{ team: "Warriors", assists: [31, 56, 76] },
{ team: "Spurs", assists: [15, 32, 64, 76, 94] }
]
each unique team in the original array of objects receives its own object in the new array, and the assist values are now the cumulative sum of the assist values. I am fairly certain that it will always be the case that the original array of objects is ordered in the correct manner, such that looping top to bottom will produce the correct cumulative sum.
Any help with this is greatly appreciated, thanks!