0

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..

thomasbishop
  • 73
  • 1
  • 8
  • Somethings missing here. Did you map these arrays to moment objects somewhere? – charlietfl Apr 11 '21 at 00:05
  • 1
    Also There is no `[0][0][0]` ... `[0][0]` is the first object so `data[0][0].start` returns the first date string shown – charlietfl Apr 11 '21 at 00:11
  • Should `start: 2020-01-01T02:45:00.000Z` be `start: "2020-01-01T02:45:00.000Z"` and so on? I.e. are the values Date objects or strings? – RobG Apr 11 '21 at 03:48
  • So you want to find all the overlaps between sub arrays, then find the common range across all overlaps? Should the result be just the final overlap? What should be done with overlaps that don't overlap with other overlaps? – RobG Apr 11 '21 at 03:59
  • There is an overlap between [0][0] and [1][0] that is 4:15 to 4:30, and an overlap between [0][1] and [1][1] that is 0:15 to 0:130. Those two overlaps don't overlap, so do you keep both overlaps? There are a total of 8 overlaps, how should the results be presented? As @charlietfl says, the references don't make sense, there is no index of 3 in any of the arrays (that would require a minimum length of 4). The largest index in any array is 2. – RobG Apr 11 '21 at 05:22
  • what is your expected output result ? – Nur Apr 11 '21 at 13:22
  • @Nur: sets of time when at least two from each time interval block overlap. So with the example above, the outcome would be: 00:15 - 01:30, 02:00 - 03:30, 04:00 - 04:30 – thomasbishop Apr 11 '21 at 13:33

0 Answers0