有人可以帮我算法吗?
我有一个事件列表,我希望按当前日期获取下一个事件和之前的事件。
示例
我从sql数据库中获取所有事件,如下所示:
events = [
{eventId:1, eventDate: "Wen Apr 01 2015 18:41:00 GMT+0300", eventPlace":'Dortmund, DE'},
{eventId:2, eventDate: "Sun Apr 05 2015 23:41:00 GMT+0300", eventPlace":'Budapest, HU'},
{eventId:3, eventDate: "Fri Apr 03 2015 13:41:00 GMT+0300" eventPlace":'Madrid, ES'},
{eventId:4, eventDate: "Mon Jun 01 2015 22:00:00 GMT+0300" eventPlace":'London, EN'},
.......
{eventId:100, eventDate: "Mon Aug 31 2015 22:00:00 GMT+0300"}
]
如果当前日期是2015年4月5日星期日15:00:00我想要
resultNextEv= {eventId:2, eventDate: "Sun Apr 05 2015 23:41:00 GMT+0300", eventPlace":'Budapest, HU'}
resultLastEv= {eventId:3, eventDate: "Fri Apr 03 2015 13:41:00 GMT+0300" eventPlace":'Madrid, ES'}
获得此结果的最佳方法是什么? (我使用javascript / angular) 有什么想法吗?
由于
答案 0 :(得分:1)
首先,按日期对事件进行排序:
var sorted= events.sort(function(a,b) {
return new Date(a.eventDate) - new Date(b.eventDate);
});
然后,遍历已排序的事件。如果日期小于目标日期,请将其设置为上一个事件。如果它大于目标日期,请将其设置为下一个事件,并停止迭代:
function getDates() {
var sorted= events.sort(function(a,b) {
return new Date(a.eventDate) - new Date(b.eventDate);
});
var date= new Date(document.getElementById('date').value),
i,sdate;
for(i = 0 ; i < sorted.length ; i++) {
sdate= new Date(sorted[i].eventDate);
if(sdate > date) break;
}
//do something with sorted[i-1] and sorted[i]
} //getDates
要获取最后的 N 事件,请使用sorted[i-1]
,sorted[i-2]
,... sorted[i-N]
。
要获取下一个 N 事件,请使用sorted[i]
,sorted[i+1]
,... sorted[i+N-1]
。
答案 1 :(得分:1)
var events = [
{eventId:1, eventDate: "Wen Apr 01 2015 18:41:00 GMT+0300", eventPlace:"Dortmund, DE"},
{eventId:2, eventDate: "Sun Apr 05 2015 23:41:00 GMT+0300", eventPlace:"Budapest, HU"},
{eventId:3, eventDate: "Fri Apr 03 2015 13:41:00 GMT+0300", eventPlace:"Madrid, ES"},
{eventId:4, eventDate: "Mon Jun 01 2015 22:00:00 GMT+0300", eventPlace:"London, EN"}
];
// sort the events
var sortedEvents = events.sort(function(a, b) {
return new Date(a.eventDate) - new Date(b.eventDate);
});
var now = new Date("Sun Apr 05 2015 15:00:00");
var futureEvents = [];
var previousEvents = [];
// finding out the next event. Go from the earliest to the newest. When you find the first later than "now", get it
for(var i = 0; i < sortedEvents.length; i++){
if(new Date(sortedEvents[i].eventDate) > now) {
futureEvents.push(sortedEvents[i]);
}
}
// finding out the previous event. Go from the newest to the earliest and when you find the first earlier than "now", get it
for(var i = sortedEvents.length - 1; i >= 0; i--){
if(new Date(sortedEvents[i].eventDate) < now) {
previousEvents.push(sortedEvents[i]);
}
}
console.log("now: " + now);
console.log("next event: " + futureEvents.length ? futureEvents[0].eventDate : undefined);
console.log("previous event: " + previousEvents.length ? previousEvents[0].eventDate : undefined);
Codepen:http://codepen.io/andrerpena/pen/WbBwbG
如果您希望获得多个未来事件或多个previos事件,只需遍历futureEvents
或previousEvents
即可。例如,接下来的两个事件是futureEvents[0]
和futureEvents[1]
答案 2 :(得分:0)
以下是使用开源项目jinqJs
进行操作的方法
var events = [
{eventId:1, eventDate: "Wen Apr 01 2015 18:41:00 GMT+0300", eventPlace:'Dortmund, DE'},
{eventId:2, eventDate: "Sun Apr 05 2015 23:41:00 GMT+0300", eventPlace:'Budapest, HU'},
{eventId:3, eventDate: "Fri Apr 03 2015 13:41:00 GMT+0300", eventPlace:'Madrid, ES'},
{eventId:4, eventDate: "Mon Jun 01 2015 22:00:00 GMT+0300", eventPlace:'London, EN'},
{eventId:100, eventDate: "Mon Aug 31 2015 22:00:00 GMT+0300", eventPlace:'London, EN'}
]
;
var result1 = jinqJs().from(events).orderBy('eventDate').where(function(row){return Date.parse(row.eventDate) < Date.parse('Sun Apr 05 2015 15:00:00');}).top(1).select();
var result2 = jinqJs().from(events).orderBy('eventDate').where(function(row){return Date.parse(row.eventDate) > Date.parse('Sun Apr 05 2015 15:00:00');}).bottom(1).select();
var result = jinqJs().from(result1,result2).select();
document.body.innerHTML += '<pre>' + JSON.stringify(result, null, 2) + '</pre><br><br>';
&#13;
<script src="https://rawgit.com/fordth/jinqJs/master/jinqjs.js"></script>
&#13;