Я реализовал функцию для вас, см. Код ниже, пожалуйста
// calculate if 2 events overlap, no matter of their provided order
function overlap(eventA, eventB) {
// sort input
const [first, second] = [eventA, eventB].sort((a, b) => a.start - b.start);
// check if events overlap
const overlap = first.end > second.start;
return overlap;
}
// create a map object with the groups
// also sorted by time
// each group contains the event objects
function findOverlaps(events, query) {
// result
const groups = {};
// event to build the groups for
const queryEvent = events.find(e => e.title === query);
// the other events to check of they overlap with the queried event
const eventsWithoutQuery = events.filter(e => e.title !== query);
eventsWithoutQuery.forEach(event => {
const overlaps = overlap(event, queryEvent);
// if there is an overlap
if (overlaps) {
// construct the groupname
const groupName = `${event.start}-${event.end}`;
// if the group alread exists use it, otherwise create an empty group (array)
const group = groups[groupName] ? groups[groupName] : [];
group.push(event);
// put the group back to the groups map and name it accordingly
groups[groupName] = group;
}
});
// make sure the keys in the group are sorted (optional)
const sortedKeys = Object.keys(groups).sort();
const sortedGroups = sortedKeys.reduce(
(acc, curr) => ({
...acc,
[curr]: groups[curr]
}),
{}
);
// return sorted map
return sortedGroups;
}
// the events
let events = [
{
id: 9,
start: 0,
end: 60,
title: "Digital meeting"
},
{
id: 80,
start: 30,
end: 90,
title: "Book meeting"
},
{
id: 81,
start: 30,
end: 90,
title: "Personal meeting"
},
{
id: 84,
start: 0,
end: 24,
title: "Support"
}
];
console.log("Iterate over Digital meeting groups");
// construct the groups for "Digial meeting"
const groups = findOverlaps(events, "Digital meeting");
// iterate over overlap maps of Digital meeting
for (const [groupName, eventsOfGroup] of Object.entries(groups)) {
console.log(groupName, eventsOfGroup);
}
console.log("Iterate over everything");
events.forEach(e => {
console.log("Groups for: " + e.title);
for (const [groupName, eventsOfGroup] of Object.entries(findOverlaps(events, e.title))) {
console.log(groupName, eventsOfGroup);
}
});