What I've got is an array of objects, like so
const data = [
{
group: "Group 1",
category: "G1 Cat 1",
value: 50,
budgetid: "53780a3b-ad3a"
},
{
group: "Group 1",
category: "G1 Cat 1",
value: 11,
budgetid: "53780a3b-ad3a"
},
{
group: "Group 1",
category: "G1 Cat 1",
value: 50,
budgetid: "eece3355-921e"
},
{
group: "Group 1",
category: "G1 Cat 1",
budgetid: "1a593f25-28ee",
value: 25
},
{
group: "Group 1",
category: "G1 Cat 2",
value: 5,
budgetid: "53780a3b-ad3a"
},
{
group: "Group 1",
category: "G1 Cat 2",
value: 50,
budgetid: "eece3355-921e"
},
{
group: "Group 1",
category: "G1 Cat 3",
budgetid: "1a593f25-28ee",
value: 25
},
{
group: "Group 1",
category: "G1 Cat 3",
budgetid: "1a593f25-28ee",
value: 1
},
{
group: "Group 2",
category: "G2 Cat 1",
value: 50,
budgetid: "53780a3b-ad3a"
},
{
group: "Group 2",
category: "G2 Cat 1",
value: 5,
budgetid: "53780a3b-ad3a"
},
{
group: "Group 2",
category: "G2 Cat 2",
value: 28,
budgetid: "eece3355-921e"
}
];
What I want to create is an object looking like this:
const newSet = {
rows: [
{
group: 'Group 1',
categories: [
{
category: 'G1 Cat 1',
monthlyValues: [{'53780a3b-ad3a': 61}, {'eece3355-921e': 50}, {'1a593f25-28ee': 25}]
},
{
category: 'G1 Cat 2',
monthlyValues: [{'53780a3b-ad3a': 5}, {'eece3355-921e': 50}, {'1a593f25-28ee': 26}]
}
]
},
{
group: 'Group 2',
categories: [
{
category: 'G2 Cat 1',
monthlyValues: [{'53780a3b-ad3a': 55}]
},
{
category: 'G2 Cat 2',
monthlyValues: [{'eece3355-921e': 28}]
},
]
}
]
}
So the data is grouped by its groups, then each group has categories that are unique and finally each category has a monthly total value which is sum of the values for the same budgetid in each group/category. What is also important is that the key for the total values needs to be data.budgetid and in string format as it contains hyphens and numbers.