I have a set of data structured as follows:
[{ team: 'Team1',
wins: 1,
group: GroupA
},{ team: 'Team2',
wins: 1,
group: GroupA
},{ team: 'Team3',
wins: 1,
group: GroupB
},{ team: 'Team4',
wins: 1,
group: GroupB
}]
My goal is to transform this data into a new format where it is grouped by a certain value, such as "group", and that value serves as a key in an object containing arrays:
{ GroupA: [{'Team1', wins: 1, group: GroupA},{'Team2', wins: 1, group: GroupA}],
GroupB: [{'Team3', wins: 1, group: GroupB},{'Team4', wins: 1, group: GroupB}]
}
Can anyone provide guidance on how I can achieve this?
I attempted the following approach which almost worked, but ended up returning objects instead:
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);
}
}