我有一个包含数据的json数组。我希望按周,月和日对其中的数据进行分组。年。
我尝试按照Javascript groupby
进行讨论如何获得按月和按年分组的值总和?请问下面的预期产量如何?
按照引用的示例,我得到如下输出
每月分组的输出
每年分组的输出
这里有数据&代码
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);
}
我可以请求帮助吗?
输出将如下生成
每周聚合数据
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
}]
每月汇总的数据
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
}]
年度合并数据
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
}]
答案 0 :(得分:1)
您的代码中没有像data
那样定义的内容。
你应该做的就像是。
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 get your desired result you can write this in a function
var groupedArray=[];
for ( 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)