Skip to content
Advertisement

How to filter list of objects that doesn’t match some condition in another list of objects

I have an array of objects of optional working hours of some business (5 min jumps).

In addition, I have an array of objects of appointments.

I want to filter the optional working hours if there is an existing appointment between those hours.

I tried this solution with moment.js library:

  const optionalWorkingHours = [
    { from: "05:00", to: "05:05" },
    { from: "05:05", to: "05:10" },
    { from: "05:10", to: "05:15" },
    { from: "05:15", to: "05:20" },
    // ....
  ];

  const appointments = [
    { from: "05:00", to: "05:05" },
    { from: "05:30", to: "06:00" },
    { from: "07:50", to: "08:00" }
  ];

  const availableHours = optionalWorkingHours.filter((o) => {
    const optionalFrom = moment(o.from, "HH:mm");
    const optionalTo = moment(o.to, "HH:mm");

    return appointments.some((c) => {
      const currentFrom = moment(c.from, "HH:mm");
      const currentTo = moment(c.to, "HH:mm");

      const isBusyHours =
        currentFrom.isSameOrAfter(optionalFrom, "hour") &&
        currentTo.isSameOrBefore(optionalTo, "hour");

      return !isBusyHours;
    });
  });

Current result: Duplicated available working hours

Expected result: Array of available working hours that don’t overlap with appointments array.

Advertisement

Answer

It’s not so easy, but your check in .some() it wasn’t complete in my opinion. Can you try whit that solution:

const optionalWorkingHours = [
  { from: "05:00", to: "05:05" }, // no
  { from: "05:05", to: "05:10" },
  { from: "05:10", to: "05:15" },
  { from: "05:15", to: "05:20" },
  { from: "05:20", to: "05:25" },
  { from: "05:25", to: "05:30" },
  { from: "05:30", to: "05:35" }, // no
  { from: "05:35", to: "05:40" }, // no
  { from: "05:40", to: "05:45" }, // no
  { from: "05:45", to: "05:50" }, // no
  { from: "05:50", to: "05:55" }, // no
  { from: "05:55", to: "06:00" }, // no
  { from: "06:00", to: "06:05" }, //
  // ....
];

const appointments = [
  { from: "05:00", to: "05:05" },
  { from: "05:30", to: "06:00" },
  { from: "07:50", to: "08:00" }
];

const availableHours = optionalWorkingHours.filter((o) => {
  const optionalFrom = moment(o.from, "HH:mm");
  const optionalTo = moment(o.to, "HH:mm");

  const check = appointments.some((appointment) => {
    const appointmentFrom = moment(appointment.from, "HH:mm");
    const appointmentTo = moment(appointment.to, "HH:mm");

    const isBusyHours =
      (optionalFrom.isSame(appointmentFrom) && optionalTo.isSame(appointmentTo)) ||
      (optionalFrom.isSame(appointmentFrom) && optionalTo.isBetween(appointmentFrom, appointmentTo)) ||
      (optionalFrom.isBetween(appointmentFrom, appointmentTo) && optionalTo.isSame(appointmentTo)) ||
      (optionalFrom.isBetween(appointmentFrom, appointmentTo) && optionalTo.isBetween(appointmentFrom, appointmentTo))


    return isBusyHours;
  });

  return !check;
});

console.table(availableHours);
console.log(availableHours);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
Advertisement