jquery - Javascript groupby week/month/year -
i have json array data in it. i'd group data in week, month & year.
i tried following discussion javascript groupby
how can sum of values grouped month & year in expected output show below please?
following example quoted, output below
monthly grouping's output
yearly grouping's output
here's data & code
function displaycalc() { var data= [{ "event_date": "2015-06-27", "number_events": 3, "full_event_duration": 42.24, "first_pause": 42.24, "second_pause": 14.08, "third_pause": 42.24, "fourth_pause": 42.24, "fifth_pause": 42.24, "sixth_pause": 42.24 }, { "event_date": "2015-06-28", "number_events": 3, "full_event_duration": 27.54, "first_pause": 27.54, "second_pause": 9.18, "third_pause": 27.54, "fourth_pause": 27.54, "fifth_pause": 27.54, "sixth_pause": 27.54 },{ "event_date": "2015-07-02", "number_events": 39, "full_event_duration": 354.39, "first_pause": 26.19, "second_pause": 9.08, "third_pause": 29.96, "fourth_pause": 32.79, "fifth_pause": 35, "sixth_pause": 36.47 }, { "event_date": "2015-07-03", "number_events": 66, "full_event_duration": 593, "first_pause": 26.26, "second_pause": 8.98, "third_pause": 29.21, "fourth_pause": 31.24, "fifth_pause": 33.21, "sixth_pause": 34.56 },{ "event_date": "2015-08-01", "number_events": 6, "full_event_duration": 57.21, "first_pause": 28.6, "second_pause": 9.53, "third_pause": 35.33, "fourth_pause": 36.17, "fifth_pause": 36.68, "sixth_pause": 37.02 }, { "event_date": "2015-08-02", "number_events": 12, "full_event_duration": 94.47, "first_pause": 23.22, "second_pause": 7.87, "third_pause": 25.1, "fourth_pause": 25.36, "fifth_pause": 25.51, "sixth_pause": 25.62 }, { "event_date": "2015-09-01", "number_events": 12, "full_event_duration": 88.55, "first_pause": 6.84, "second_pause": 7.37, "third_pause": 9.18, "fourth_pause": 9.82, "fifth_pause": 10.21, "sixth_pause": 10.48 }, { "event_date": "2015-09-03", "number_events": 15, "full_event_duration": 116.13, "first_pause": 6.95, "second_pause": 7.74, "third_pause": 9.52, "fourth_pause": 10.99, "fifth_pause": 12.39, "sixth_pause": 13.33 }]; var grouped = _.groupby(data, function (item) { return item.event_date; }); var groupedbyyear = _.groupby(data, function (item) { return item.event_date.substring(0, 4); }); var groupedbymonth = _.groupby(data, function (item) { return item.event_date.substring(0, 7); }); console.log(grouped); console.log(groupedbyyear); console.log(groupedbymonth); }
could request achieve please?
the output generated below
weekly aggregated data
var jsondata = [{ "event_date": "2015-01-04", "number_events": 30, "full_event_duration": 100.27, "first_pause": 142.24, "second_pause": 143.08, "third_pause": 305.24, "fourth_pause":271.24, "fifth_pause": 289.24, "sixth_pause": 238.24 }, { "event_date": "2015-01-11", "number_events": 300, "full_event_duration": 131.27, "first_pause": 124.24, "second_pause": 347.08, "third_pause": 245.24, "fourth_pause":345.24, "fifth_pause": 168.24, "sixth_pause": 458.24 }, { "event_date": "2015-01-18", "number_events": 49, "full_event_duration": 456.27, "first_pause": 23.24, "second_pause": 37.08, "third_pause": 678.24, "fourth_pause":345.24, "fifth_pause": 123.24, "sixth_pause": 123.24 }]
monthly aggregated data
var jsondata = [{ "event_date": "2015-01", "number_events": 30, "full_event_duration": 100.27, "first_pause": 142.24, "second_pause": 143.08, "third_pause": 305.24, "fourth_pause":271.24, "fifth_pause": 289.24, "sixth_pause": 238.24 }, { "event_date": "2015-02", "number_events": 300, "full_event_duration": 131.27, "first_pause": 124.24, "second_pause": 347.08, "third_pause": 245.24, "fourth_pause":345.24, "fifth_pause": 168.24, "sixth_pause": 458.24 }, { "event_date": "2015-04", "number_events": 49, "full_event_duration": 456.27, "first_pause": 23.24, "second_pause": 37.08, "third_pause": 678.24, "fourth_pause":345.24, "fifth_pause": 123.24, "sixth_pause": 123.24 }]
yearly aggregated data
var jsondata = [{ "event_date": "2014", "number_events": 3000, "full_event_duration": 1000.27, "first_pause": 1420.24, "second_pause": 1430.08, "third_pause": 3050.24, "fourth_pause":2710.24, "fifth_pause": 2890.24, "sixth_pause": 2380.24 }, { "event_date": "2015", "number_events": 4000, "full_event_duration": 131.27, "first_pause": 1240.24, "second_pause": 3470.08, "third_pause": 2450.24, "fourth_pause":3450.24, "fifth_pause": 1680.24, "sixth_pause": 4580.24 }]
you have nothing defined data
in code. should doing like.
var jsondata = [{ "event_date": "2015-06-27", "number_events": 3, "full_event_duration": 42.24, "first_pause": 42.24, "second_pause": 14.08, "third_pause": 42.24, "fourth_pause": 42.24, "fifth_pause": 42.24, "sixth_pause": 42.24 }, { "event_date": "2015-06-28", "number_events": 3, "full_event_duration": 27.54, "first_pause": 27.54, "second_pause": 9.18, "third_pause": 27.54, "fourth_pause": 27.54, "fifth_pause": 27.54, "sixth_pause": 27.54 },{ "event_date": "2015-07-02", "number_events": 39, "full_event_duration": 354.39, "first_pause": 26.19, "second_pause": 9.08, "third_pause": 29.96, "fourth_pause": 32.79, "fifth_pause": 35, "sixth_pause": 36.47 }, { "event_date": "2015-07-03", "number_events": 66, "full_event_duration": 593, "first_pause": 26.26, "second_pause": 8.98, "third_pause": 29.21, "fourth_pause": 31.24, "fifth_pause": 33.21, "sixth_pause": 34.56 },{ "event_date": "2015-08-01", "number_events": 6, "full_event_duration": 57.21, "first_pause": 28.6, "second_pause": 9.53, "third_pause": 35.33, "fourth_pause": 36.17, "fifth_pause": 36.68, "sixth_pause": 37.02 }, { "event_date": "2015-08-02", "number_events": 12, "full_event_duration": 94.47, "first_pause": 23.22, "second_pause": 7.87, "third_pause": 25.1, "fourth_pause": 25.36, "fifth_pause": 25.51, "sixth_pause": 25.62 }, { "event_date": "2015-09-01", "number_events": 12, "full_event_duration": 88.55, "first_pause": 6.84, "second_pause": 7.37, "third_pause": 9.18, "fourth_pause": 9.82, "fifth_pause": 10.21, "sixth_pause": 10.48 }, { "event_date": "2015-09-03", "number_events": 15, "full_event_duration": 116.13, "first_pause": 6.95, "second_pause": 7.74, "third_pause": 9.52, "fourth_pause": 10.99, "fifth_pause": 12.39, "sixth_pause": 13.33 }]; var grouped = _.groupby(jsondata, function (item) { return item.event_date; }); var groupedbyyear = _.groupby(jsondata, function (item) { return item.event_date.substring(0, 4); }); var groupedbymonth = _.groupby(jsondata, function (item) { return item.event_date.substring(0, 7); }); console.log(grouped); console.log(groupedbyyear); console.log(groupedbymonth); //to desired result can write in function var groupedarray=[]; ( key in groupedbymonth){ var obj = {}; obj.event_date=key; obj.fifth_pause=0; obj.first_pause=0; obj.fourth_pause=0; obj.full_event_duration=0; obj.number_events=0; obj.second_pause=0; obj.sixth_pause=0; obj.third_pause=0; for(var i=0;i<groupedbymonth[key].length;i++){ obj.fifth_pause+=groupedbymonth[key][i].fifth_pause; obj.first_pause+=groupedbymonth[key][i].first_pause; obj.fourth_pause+=groupedbymonth[key][i].fourth_pause; obj.full_event_duration+=groupedbymonth[key][i].full_event_duration; obj.number_events+=groupedbymonth[key][i].number_events; obj.second_pause+=groupedbymonth[key][i].second_pause; obj.sixth_pause+=groupedbymonth[key][i].sixth_pause; obj.third_pause+=groupedbymonth[key][i].third_pause; } obj.fifth_pause=(obj.fifth_pause/groupedbymonth[key].length).tofixed(2); obj.first_pause=(obj.first_pause/groupedbymonth[key].length).tofixed(2); obj.fourth_pause=(obj.fourth_pause).tofixed(2); obj.full_event_duration=(obj.full_event_duration/groupedbymonth[key].length).tofixed(2); obj.number_events=(obj.number_events/groupedbymonth[key].length).tofixed(2); obj.second_pause=(obj.second_pause/groupedbymonth[key].length).tofixed(2); obj.sixth_pause=(obj.sixth_pause/groupedbymonth[key].length).tofixed(2); obj.third_pause=(obj.third_pause/groupedbymonth[key].length).tofixed(2); groupedarray.push(obj); } console.log(groupedarray)
Comments
Post a Comment