我有一些JSON数据,我需要分割然后聚合。以下是其格式的示例。
data2 = [{"startDate": 1396263600.0, "adId": 2483231759355, "endDate": 1401101940.0, "impressions": 754831},
{"startDate": 1393851600.0, "adId": 2750329551133, "endDate": 1404212340.0, "impressions": 3947368},
{"startDate": 1401620400.0, "adId": 1311595275159, "endDate": 1404212340.0, "impressions": 630000}];
我希望在日期范围内统一划分每一天的展示次数。 (即如果在10天的范围内有100次展示,那么每天将获得10次展示)。然后,我想汇总每天所有adId的展示次数(即如果一天有2个广告,那么它们会合并)。因此,最终结果将是一系列日期和印象。不太熟悉javascript所以不确定是否有任何功能可以让我的生活更轻松。有什么建议吗?
我计划使用此功能来计算日期之间的天数:
function days_between(date1, date2) {
// The number of milliseconds in one day
var ONE_DAY = 1000 * 60 * 60 * 24;
// Convert both dates to milliseconds
var date1_ms = date1 * 1000;
var date2_ms = date2 * 1000;
// Calculate the difference in milliseconds
var difference_ms = Math.abs(date1_ms - date2_ms);
// Convert back to days and return
return Math.round(difference_ms/ONE_DAY);
}
答案 0 :(得分:1)
你在考虑这样的事吗?
function days_between(date1, date2) {
var date1_ms = new Date(date1*1000).getTime();
var date2_ms = new Date(date2*1000).getTime();
// Calculate the difference in milliseconds
var difference_ms = Math.abs(date1_ms - date2_ms);
// Convert back to days and return
return Math.ceil(difference_ms / (1000 * 3600 * 24));
}
function aggregate(data) {
result = [];
for (var i = 0; i < data.length; i++) {
var numOfDays = days_between( data[i].startDate, data[i].endDate );
var adId = data[i].adId;
var impressionPerDay = (data[i].impressions / numOfDays);
for (var j = 0; j < numOfDays; j++) {
var obj = {};
obj.date = new Date(new Date(data[i].startDate*1000).getTime() + (j * (1000 * 3600 * 24))).getTime();
obj.impressions = impressionPerDay;
obj.adId = adId;
result.push(obj);
}
}
return result;
}
function aggregateAdIds(data) {
var result = {};
for (var i = 0; i < data.length; i++) {
if (data[i].adId in result) result[data[i].adId].push({"date": data[i].date, "impressions": data[i].impressions});
else result[data[i].adId] = [{"date": data[i].date, "impressions": data[i].impressions}];
}
return result;
}
var data = [{"startDate": 1396263600.0, "adId": 2483231759355, "endDate": 1401101940.0, "impressions": 754831},{"startDate": 1393851600.0, "adId": 2750329551133, "endDate": 1404212340.0, "impressions": 3947368},{"startDate": 1401620400.0, "adId": 1311595275159, "endDate": 1404212340.0, "impressions": 630000}];
var final = aggregateAdIds(aggregate(data));
<强>已更新强>
function days_between(date1, date2) {
var date1_ms = new Date(date1*1000).getTime();
var date2_ms = new Date(date2*1000).getTime();
// Calculate the difference in milliseconds
var difference_ms = Math.abs(date1_ms - date2_ms);
// Convert back to days and return
return Math.ceil(difference_ms / (1000 * 3600 * 24));
}
function aggregate(data) {
var map = {};
var result = [];
for (var i = 0; i < data.length; i++) {
var numOfDays = days_between( data[i].startDate, data[i].endDate );
console.log('days : '+ numOfDays);
var impressionPerDay = (data[i].impressions / numOfDays);
for (var j = 0; j < numOfDays; j++) {
var tempDate = new Date(data[i].startDate*1000);
tempDate.setDate(tempDate.getDate() + j);
var date = new Date(tempDate.getFullYear(), tempDate.getMonth(), tempDate.getDate(), 0, 0 ,0, 0);
if (date.getTime() in map) map[date.getTime()] += impressionPerDay;
else map[date.getTime()] = impressionPerDay;
}
}
for (var key in map) {
result.push({'date' : Number(key), 'impressions' : map[key]});
}
result.sort(function(a,b){return a.date > b.date});
return result;
}
var data = [{"startDate": 1396263600.0, "adId": 2483231759355, "endDate": 1401101940.0, "impressions": 754831},{"startDate": 1393851600.0, "adId": 2750329551133, "endDate": 1404212340.0, "impressions": 3947368},{"startDate": 1401620400.0, "adId": 1311595275159, "endDate": 1404212340.0, "impressions": 630000}];
console.log(aggregate(data));
更新小提琴:link
答案 1 :(得分:0)
这样的事情?这会将每天的展示次数计入javascript对象,每个日期以毫秒为单位(例如1403953200000:21000)。如果需要,您可以将毫秒转换为日期,21000是当天的展示次数
var result={};
for(var i=0;i<data2.length;i++){
var end = new Date(data2[i].endDate*1000);
var start = new Date(data2[i].startDate*1000);
var diff= Math.abs(end.getTime() - start.getTime());
var diffDays = Math.ceil(diff / (1000 * 3600 * 24));
for (; start <= end; start.setDate(start.getDate() + 1)) {
var miliseconds=start.getTime();
if(!result.hasOwnProperty(miliseconds)) result[miliseconds]=0;
result[miliseconds]+=data2[i].impressions/diffDays;
}
}
console.log(result)
<强>更新强>
function aggregate(data2) {
var result={};
var result_arr=[];
for(var i=0;i<data2.length;i++){
var end = new Date(data2[i].endDate*1000);
end.setHours(0);
end.setMinutes(0);
end.setSeconds(0);
var start = new Date(data2[i].startDate*1000);
start.setHours(0);
start.setMinutes(0);
start.setSeconds(0);
var diff= Math.abs(end.getTime() - start.getTime());
var diffDays = Math.ceil(diff / (1000 * 3600 * 24));
console.log(start.toString(),end.toString(), diffDays)
for (; start <= end; start.setDate(start.getDate() + 1)) {
var miliseconds=start.getTime();
if(!result.hasOwnProperty(miliseconds)) result[miliseconds]=0;
result[miliseconds]+=data2[i].impressions/diffDays;
}
}
for(var key in result){
result_arr.push({date:Number(key), impressions: result[key]});
}
result_arr.sort(function(a,b){return a.date > b.date})
return result_arr;
}
摆弄我的工作解决方案:http://jsfiddle.net/juvian/6R7mu/14/