I have data like this:
[{ team: 'Team1',
wins: 1,
group: GroupA
},{ team: 'Team2',
wins: 1,
group: GroupA
},{ team: 'Team3',
wins: 1,
group: GroupB
},{ team: 'Team4',
wins: 1,
group: GroupB
}]
I want it in this form (I basically want to group the data by some value, in this case "group" and use that value as a key in an object of arrays):
{ GroupA: [{'Team1', wins: 1, group: GroupA},{'Team2', wins: 1, group: GroupA}],
GroupB: [{'Team3', wins: 1, group: GroupB},{'Team4', wins: 1, group: GroupB}]
}
How can I accomplish this?
Here is what I tried and which almost worked, but returns objects:
var newStats = {}
arrTeamStats.map(function(key,idx){
var arr = [];
if(newStats[key.group] == undefined) {
arr = key;
newStats[key.group] = arr;
} else {
else {
group = key.group;
newStats[group].push(key);
}
}