我有一个JSON js对象,我正在使用AJAX。 问题是我对后端没有多少控制权,所以数据来自简单的JS对象。
我有一个JSON文件如下
var test =
[
{value : 23,
file_date : Wed Jan 01 2014 05:34:53 GMT+0530 (IST)
},
{value : 23,
file_date : Wed Jan 01 2014 09:34:53 GMT+0530 (IST)
},
{value : 23,
file_date : Wed Jan 01 2014 02:34:53 GMT+0530 (IST)
},
{value : 23,
file_date : Wed Jan 02 2014 06:34:53 GMT+0530 (IST)
},
{value : 23,
file_date : Wed Jan 02 2014 09:34:53 GMT+0530 (IST)
},
{value : 23,
file_date : Wed Jan 02 2014 04:34:53 GMT+0530 (IST)
},
{value : 23,
file_date : Wed Jan 03 2014 02:34:53 GMT+0530 (IST)
},
{value : 23,
file_date : Wed Jan 03 2014 01:34:53 GMT+0530 (IST)
},
{value : 23,
file_date : Wed Jan 03 2014 10:34:53 GMT+0530 (IST)
},
{value : 23,
file_date : Wed Jan 04 2014 7:34:53 GMT+0530 (IST)
},
{value : 23,
file_date : Wed Jan 05 2014 10:34:53 GMT+0530 (IST)
},
{value : 23,
file_date : Wed Jan 06 2014 11:34:53 GMT+0530 (IST)
}
]
file_ts具有不同的时间戳。我的问题是我必须整合这些数据。
如果用户选择1天,那么我必须每小时得到所有数据的总和。如果在2到3小时之间有4个值,那么我必须在数小时内对该数据求和。
然后每周一次,然后每周一次。我正在尝试使用express和mysql编写一个nodejs服务器,但它也无法正常工作。
答案 0 :(得分:0)
以下是我假设您要求的内容:
如果用户选择' 1月1日',您想要获取所有信息 file_date在所选日期,并将所有小时数相加。
以下是您需要采取的步骤:
注意:我已将您的时区(IST)转换为(UTC),因此输出将采用GMT
// Test data
var test = [{
value: 23,
file_date: "Wed Jan 01 2014 05:34:53 GMT+0530 (IST)"
},{
value: 23,
file_date: "Wed Jan 01 2014 09:34:53 GMT+0530 (IST)"
},{
value: 23,
file_date: "Wed Jan 01 2014 02:34:53 GMT+0530 (IST)"
},{
value: 23,
file_date: "Thu Jan 02 2014 06:34:53 GMT+0530 (IST)"
},{
value: 23,
file_date: "Thu Jan 02 2014 09:34:53 GMT+0530 (IST)"
},{
value: 23,
file_date: "Thu Jan 02 2014 04:34:53 GMT+0530 (IST)"
}
];
var userInput = 1; // This is the date they typed in
// This function returns all records for a specific day (integer)
var getAllDayData = function(day){
var response = [];
for(var i=0;i<test.length;i++){
var fileDate = new Date(test[i].file_date);
if(fileDate.getDate() === day){ // getDate gets you day of month
response.push(fileDate);
}
}
return response;
};
var getTotalHoursFromDates = function(data){
var totalHours = 0;
for(var i=0;i<data.length;i++){
totalHours += data[i].getHours();
}
return totalHours;
};
var getTotalHoursFromInput = function(day){
var dayData = getAllDayData(day);
var response = getTotalHoursFromDates(dayData);
return response;
};
// Call the function
var dayTotal = getTotalHoursFromInput(userInput);
console.log(dayTotal); // 58
关于每周抓取时间的请求:您必须添加一个占用一周中所有日期(输入)的函数,收集每天的所有信息,添加然后添加全部那些时间在一起。这是上述代码的延续。
var week = [31,1,2,3,4,5,6]; // This is the days of the selected full week
var getAllWeekData = function(days){
var response = [];
for(var i=0;i<days.length;i++){
var day = getAllDayData(days[i]);
if(day[0] != null) { // Checks for undefined or null
for(var y=0;y<day.length;y++){
response.push(day[i]);
}
}
}
return response;
};
var getAllWeekDays = function(selectedDays){
var allDays = getAllWeekData(selectedDays);
var response = getTotalHoursFromDates(allDays);
return response;
};
var weekTotal = getAllWeekDays(week);
console.log(weekTotal); // 120