我需要分解然后汇总一些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);
}
最佳答案
您是否正在考虑这样的事情?
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
关于javascript - 分割和汇总json数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23644411/