Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

142
Visualizações
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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda