I am having problems summarizing and grouping my object in Jquery as I would like to group by country and then have a grand total based on subperiods 4,5,and 6 for values within col1. The col1, col2, col3 are dynamic, but using this is an example.
What I need is to get the total by country which is stored in the country variable, grand total per subperiod stored in the columns variable, and the total count stored in the averages variable.
Here is my code in jsfiddle:
My object:
var g_jsonData =
[
{
"Country": "Spain",
"Year": "2000",
"Subperiod": "4",
"col1": "75",
"col2": "500",
"col3": "200"
},
{
"Country": "Spain",
"Year": "2001",
"Subperiod": "4",
"col1": "50",
"col2": "500",
"col3": "300"
},
{
"Country": "Spain",
"Year": "2002",
"Subperiod": "4",
"col1": "50",
"col2": "200",
"col3": "300"
},
{
"Country": "Spain",
"Year": "2003",
"Subperiod": "4",
"col1": "",
"col2": "200",
"col3": "300"
},
{
"Country": "Spain",
"Year": "2005",
"Subperiod": "5",
"col1": "125",
"col2": "500",
"col3": "300"
},
{
"Country": "Spain",
"Year": "2012",
"Subperiod": "6",
"col1": "100.75",
"col2": "500",
"col3": "200"
},
{
"Country": "Spain",
"Year": "2013",
"Subperiod": "6",
"col1": "200",
"col2": "500",
"col3": "300"
},
{
"Country": "France",
"Year": "2000",
"Subperiod": "4",
"col1": "100",
"col2": "100",
"col3": "300"
},
{
"Country": "France",
"Year": "2001",
"Subperiod": "4",
"col1": "100",
"col2": "100",
"col3": "300"
},
{
"Country": "France",
"Year": "2002",
"Subperiod": "4",
"col1": "100",
"col2": "200",
"col3": "300"
},
{
"Country": "France",
"Year": "2005",
"Subperiod": "5",
"col1": "100",
"col2": "200",
"col3": "300"
},
{
"Country": "France",
"Year": "2012",
"Subperiod": "6",
"col1": "100",
"col2": "100",
"col3": "300"
},
{
"Country": "France",
"Year": "2013",
"Subperiod": "6",
"col1": "100",
"col2": "100",
"col3": "300"
}];
Here is the end result which I would like to get and this is based on summarizing col1 per subperiod per country.
countries =
{
"Spain":{
"4":175,
"5":125,
"6":300.75
},
"France":{
"4":300,
"5":100,
"6":200,
}
}
Grand total for the subperiods 4,5,6 should show as:
columns =
{
"4":475,
"5":225,
"6":500.75
}
I would also like to get the total count per country, per subperiod:
averages =
{
"Spain":{
"4":3,
"5":1,
"6":2
},
"France":{
"4":3,
"5":1,
"6":2
}
}
I would like to try to stick to my code as close as possible. Thanks, appreciate the help.