0

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.

dariusz
  • 441
  • 1
  • 5
  • 17
  • What have you tried so far to solve this on your own? You will find for every sub-problem a solution here on SO -> [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – Andreas Jan 17 '22 at 16:48
  • Please visit [help], take [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output, preferably in a [Stacksnippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) – mplungjan Jan 17 '22 at 16:50

0 Answers0