Skip to content
Advertisement

Select next object based on Date

I’m trying to Loop through an array and pull out the closest/next meeting object Chronologically.

Each object has a Start and End time but I want to know what is the current event OR the next event that’s going to happen. Failing that to get the last event.

e.g.

const meetings = [{title:"morning",startAt:new Date("2020-10-14 7:00"),
                                     endAt:new Date("2020-10-14 8:00")},
                 {title:"evening",startAt:new Date("2020-10-14 20:00"),
                                    endAt:new Date("2020-10-14 21:00")},
                 {title:"afternoon",startAt:new Date("2020-10-14 15:00"),
                                    endAt:new Date("2020-10-14 15:12")}]
// results:
"2020-10-14 01:00" // morning
"2020-10-14 07:30" // morning
"2020-10-14 08:01" // afternoon
"2020-10-14 15:00" // afternoon
"2020-10-14 15:12" // afternoon
"2020-10-14 15:13" // evening
"2020-10-14 21:00" // evening
"2020-10-14 23:59" // evening

This is what i have so far:

const targetDate = new Date("2020-10-14 15:12")

meetings.reduce((found,meeting)=>{
           if( ! found){
                return meeting
           } else if( meeting.startAt > targetDate
                   &&( found.startAt < targetDate
                   ||  meeting.startAt < found.startAt)){
                 return meeting
           }
           return found
      },{}))

Advertisement

Answer

Try this function getMeeting(meetings,targetDate)

You can pass the set of meetings + a target Date/defaults to now

const testSet = [
                [new Date("2020-10-14 01:00"),"morning"],
                [new Date("2020-10-14 07:30"),"morning"],
                [new Date("2020-10-14 08:01"),"afternoon"],
                [new Date("2020-10-14 15:00"),"afternoon"],
                [new Date("2020-10-14 15:12"),"afternoon"],
                [new Date("2020-10-14 15:13"),"evening"],
                [new Date("2020-10-14 21:00"),"night"],
                [new Date("2020-10-14 23:59"),"night"],
              ]
              
    const meetings = [
      { title: "morning", startAt: new Date("2020-10-14 7:00"), endAt: new Date("2020-10-14 8:00") },
      { title: "evening", startAt: new Date("2020-10-14 20:00"), endAt: new Date("2020-10-14 20:59") },
      { title: "afternoon", startAt: new Date("2020-10-14 15:00"), endAt: new Date("2020-10-14 15:12") },
      { title: "night",  startAt: new Date("2020-10-14 21:00"),  endAt: new Date("2020-10-14 23:15") },
    ];

    
function getMeeting(meetings, targetDate = new Date()){

return meetings.sort((a,b)=>b.startAt - a.startAt)
                            .reduce((closest, meeting) => {
     
        if ( ! closest) {
          closest = meeting;
        } else if (closest.startAt <= targetDate 
               &&  closest.endAt >= targetDate) {
          // KEEP closest
        } else if (meeting.startAt <= targetDate 
               &&  meeting.endAt >= targetDate) {
          closest = meeting;
        } else if (meeting.startAt <= targetDate 
               &&  closest.startAt <= meeting.startAt) {
          closest = meeting;
        } else if (meeting.endAt >= targetDate 
               &&  closest.endAt >= meeting.endAt) {
          closest = meeting;
        }
      
      return closest;
    }, null);
}

testSet.forEach(([date,name])=>{
    console.log(name === getMeeting(meetings,date).title ? "✔" : "✘", date)
})
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement