Home > Enterprise >  Finding objects that are within a time range of each other
Finding objects that are within a time range of each other

Time:10-12

I am using an external API to fetch a list of events happening between two dates. I have then used array.reduce to group the events happening on the same day into one array.

const time = events && events.reduce((acc, item) => {
    if (!acc[item.fixture.date.split('T')[1]]) {
      acc[item.fixture.date.split('T')[1]] = [];
    }

    acc[item.fixture.date.split('T')[1]].push(item);
    return acc;
  }, {})

They are labelled by the time in which the event occurs. If I console.log time then you can see in the image below how the data is returned for one day.

Example of returned data

I am trying to work out how to loop through these objects and find the ones that are within 30 minutes of each other. For example: It would find 16:05:00 01:00 and 16:30:00 01:00 and place these into a new array called Interval together.

What would be the easiest way to achieve this?

CodePudding user response:

const datesInRange = (date1, date2, range) => //range = difference in minutes
  (date1 - date2) / 1000 / 60 <= range 
  ? true : false
  • Related