I have the following array:
var results = [
{ group: 1,
department: 2,
total: 10 },
{ group: 1,
department: 2,
total: 25 },
{ group: 1,
department: 3,
total: 15 },
{ group: 1,
department: 3,
total: 5 },
{ group: 2,
department: 4,
total: 10},
{ group: 2,
department: 4,
total: 50 }
]
I need to arrange the array into a 2D nested array, where the sub arrays are Group 1 and Group 2. The sub arrays should also be parent arrays, whose children are ordered by Department, like so:
var results:[
{
group1:[
{
department2:[
{
group:1,
department:2,
total:10
},
{
group:1,
department:2,
total:25
},
{
group:1,
department:3,
total:15
}
]
},
{
department3:[
{
group:1,
department:3,
total:5
}
]
}
]
},
{
group2:[
{
department4:[
{
group:2,
department:4,
total:10
},
{
group:2,
department:4,
total:50
}
]
}
]
}
]
This is for a react component, where I need to print each one as a row, but add a total after all the department groups are done, and then one after each group, and then a total after everything.
Perhaps I´m over complicating it and there is a better way?