Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

javascript - How can i group an array of date ranges into smaller groups with a minimum time gap?

Lets say i have an array of objects which features a start and end date:

var events = [
    {start: '2021-01-25 10:00:00', end: '2021-01-25 11:00:00', title: 'Event #1'},
    {start: '2021-01-25 10:00:00', end: '2021-01-25 12:00:00', title: 'Event #2'},
    {start: '2021-01-25 18:00:00', end: '2021-01-25 19:00:00', title: 'Event #3'},
    {start: '2021-01-25 19:00:00', end: '2021-01-25 20:00:00', title: 'Event #4'},
    {start: '2021-01-25 19:30:00', end: '2021-01-25 20:30:00', title: 'Event #5'},
    {start: '2021-01-25 23:00:00', end: '2021-01-25 23:30:00', title: 'Event #6'},
]

As you can see, the time ranges might partially overlap between records. The dates are Date objects.

I want to group these records into smaller groups, where each group is at least X hours apart. Lets say the gap is 5 hours, in that case my expected result would be this:

var event_groups = [
    [
        {start: '2021-01-25 10:00:00', end: '2021-01-25 11:00:00', title: 'Event #1'},
        {start: '2021-01-25 10:00:00', end: '2021-01-25 12:00:00', title: 'Event #2'},
    ],
    [
        {start: '2021-01-25 18:00:00', end: '2021-01-25 19:00:00', title: 'Event #3'},
        {start: '2021-01-25 19:00:00', end: '2021-01-25 20:00:00', title: 'Event #4'},
        {start: '2021-01-25 19:30:00', end: '2021-01-25 20:30:00', title: 'Event #5'},
        {start: '2021-01-25 23:00:00', end: '2021-01-25 23:30:00', title: 'Event #6'},
    ]
]

If the gap is 2 hours, the results would change like this:

var event_groups = [
    [
        {start: '2021-01-25 10:00:00', end: '2021-01-25 11:00:00', title: 'Event #1'},
        {start: '2021-01-25 10:00:00', end: '2021-01-25 12:00:00', title: 'Event #2'},
    ],
    [
        {start: '2021-01-25 18:00:00', end: '2021-01-25 19:00:00', title: 'Event #3'},
        {start: '2021-01-25 19:00:00', end: '2021-01-25 20:00:00', title: 'Event #4'},
        {start: '2021-01-25 19:30:00', end: '2021-01-25 20:30:00', title: 'Event #5'},
    ],
    [
        {start: '2021-01-25 23:00:00', end: '2021-01-25 23:30:00', title: 'Event #6'},
    ]
]

As for where i'm standing so far with my code(its not much...)

var event_groups = [];
var event_group = [];
events.forEach(function(event){
    
  //If its an empty group, just add the data and move forward
    if(!event_group.length) {
    event_group.push(event);
    continue;
  }

})

Any ideas how should i solve this problem?

question from:https://stackoverflow.com/questions/65903806/how-can-i-group-an-array-of-date-ranges-into-smaller-groups-with-a-minimum-time

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could get the delta of time and check against the wanted length for building a new group.

const
    group = (array, hours) => array.reduce((r, o) => {
        if (!r.length || o.start - r[r.length - 1][0].start > hours * 1000 * 60 * 60) r.push([o]);
        else r[r.length - 1].push(o);
        return r;
    }, []),
    events = [{ start: '2021-01-25 10:00:00', end: '2021-01-25 11:00:00', title: 'Event #1' }, { start: '2021-01-25 10:00:00', end: '2021-01-25 12:00:00', title: 'Event #2' }, { start: '2021-01-25 18:00:00', end: '2021-01-25 19:00:00', title: 'Event #3' }, { start: '2021-01-25 19:00:00', end: '2021-01-25 20:00:00', title: 'Event #4' }, { start: '2021-01-25 19:30:00', end: '2021-01-25 20:30:00', title: 'Event #5' }, { start: '2021-01-25 23:00:00', end: '2021-01-25 23:30:00', title: 'Event #6' }]
        .map(o => ({ ...o, start: new Date(o.start), end: new Date(o.end) }));

console.log(group(events, 5));
console.log(group(events, 2));
.as-console-wrapper { max-height: 100% !important; top: 0; }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...