I am trying to do two things with a data structure that is quite complex.
For the following structure:
[
[
{ start: 2020-01-01T02:45:00.000Z, end: 2020-01-01T04:30:00.000Z },
{ start: 2020-01-01T00:15:00.000Z, end: 2020-01-01T02:00:00.000Z }
],
[
{ start: 2020-01-01T04:15:00.000Z, end: 2020-01-01T04:30:00.000Z },
{ start: 2020-01-01T00:15:00.000Z, end: 2020-01-01T01:30:00.000Z },
{ start: 2020-01-01T02:00:00.000Z, end: 2020-01-01T03:30:00.000Z }
],
[
{ start: 2020-01-01T02:00:00.000Z, end: 2020-01-01T03:30:00.000Z },
{ start: 2020-01-01T04:00:00.000Z, end: 2020-01-01T04:45:00.000Z },
{ start: 2020-01-01T00:45:00.000Z, end: 2020-01-01T01:15:00.000Z }
]
]
I want to find all the cases where at least two elements of the sub-arrays (ie [0][0], [0][1], [0,3]
overlap in time with each other.
For example, we can see that [0][0][0] and [0][1][0] and [0][2][1] overlap. Then I want to reduce to the intersecting time, i.e 04:00 - 04:30. But not within the same array, only accross the three on the second dimension.
So far I have come up with this algorithm but it is failing:
function seekOverlaps(arr) {
const overlapStore = [];
const determineOverlaps = function () {
for (let i = 0; i < arr.length; i++) {
let running = arr.length - i;
if (running === 1) {
arr.shift();
break;
}
overlapStore.push({
intersection: [arr[0].intersect(arr[i + 1])],
});
}
return arr;
};
while (arr.length > 1) {
determineOverlaps(arr);
}
return overlapStore;
}
Where intersect
is a Moment.js range..