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

179
Visualizações
Comparing two arrays of objects and removing when two match

I was wondering if anyone could help me. I am using React and I have two arrays of objects:

  • one for all possible booking times
  • the other for existing bookings.

I am struggling to find a way of successfully looping over the allSlots array and removing any that have a matching time inside the existingBookings array.

In the example below, there are existing bookings at 10:00am, 10:40am, 11:00am and 11:20am.

The expected output would only leave 10:20am and 11:40am from the original array.

    const allSlots = [
    {
      date: "28 Sept 22",
      time: "10:00am"
    },
    {
      date: "28 Sept 22",
      time: "10:20am"
    },
    {
      date: "28 Sept 22",
      time: "10:40am"
    },
    {
      date: "28 Sept 22",
      time: "11:00am"
    },
    {
      date: "28 Sept 22",
      time: "11:20am"
    },
    {
      date: "28 Sept 22",
      time: "11:40am"
    }
  ];

    const existingBookings = [
    {
      time: "10:00am",
      propertyID: "XQPvl7MmLVNtxHdSRfDq",
      userID: "Bq4b3uz129aE2D5TCbaOiLQJrvC2",
      date: "28 Sept 22"
    },
    {
      time: "11:00am",
      propertyID: "XQPvl7MmLVNtxHdSRfDq",
      userID: "Ko2LdnQAdaE2OiLQJrvC2D5TCbA",
      date: "28 Sept 22"
    },
    {
      time: "10:40am",
      propertyID: "XQPvl7MmLVNtxHdSRfDq",
      userID: "Ko2LdnQAdaE2OiLQJrvC2D5TCbA",
      date: "28 Sept 22"
    },
    {
      time: "11:20am",
      propertyID: "XQPvl7MmLVNtxHdSRfDq",
      userID: "iLQJrKo2LdCbnQAdaE2OvC2D5TA",
      date: "28 Sept 22"
    }
  ];

I originally filtered the existingBookings data down to remove any that did not match the selected date with:

const existingBookings = allBookings.filter(
  (booking) => booking.date === selectedDate
);

However, I am struggling to manipulate things further. I really appreciate any insight and help you may be able to give.

almost 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

This should work.

  const newAllSlots = allSlots.filter((slot)=>{
    const iSInExistingBookings = existingBookings.find(booking=>booking.time === slots.time && booking.date === slots.date)
    return !iSInExistingBookings
  })
almost 4 years ago · Santiago Trujillo Relatório

0

It is as simple as doing

freeSlots=allSlots.filter(s=>
 existingBookings.every(e=>e.time!=s.time||e.date!=s.date)
)

Here is a working (plain) JavaScript snippet:

const allSlots = [
{
  date: "28 Sept 22",
  time: "10:00am"
},
{
  date: "28 Sept 22",
  time: "10:20am"
},
{
  date: "28 Sept 22",
  time: "10:40am"
},
{
  date: "28 Sept 22",
  time: "11:00am"
},
{
  date: "28 Sept 22",
  time: "11:20am"
},
{
  date: "28 Sept 22",
  time: "11:40am"
}
  ];

const existingBookings = [
{
  time: "10:00am",
  propertyID: "XQPvl7MmLVNtxHdSRfDq",
  userID: "Bq4b3uz129aE2D5TCbaOiLQJrvC2",
  date: "28 Sept 22"
},
{
  time: "11:00am",
  propertyID: "XQPvl7MmLVNtxHdSRfDq",
  userID: "Ko2LdnQAdaE2OiLQJrvC2D5TCbA",
  date: "28 Sept 22"
},
{
  time: "10:40am",
  propertyID: "XQPvl7MmLVNtxHdSRfDq",
  userID: "Ko2LdnQAdaE2OiLQJrvC2D5TCbA",
  date: "28 Sept 22"
},
{
  time: "11:20am",
  propertyID: "XQPvl7MmLVNtxHdSRfDq",
  userID: "iLQJrKo2LdCbnQAdaE2OvC2D5TA",
  date: "28 Sept 22"
}
  ];

const freeSlots=allSlots.filter(s=>
 existingBookings.every(e=>e.time!=s.time||e.date!=s.date)
)

console.log(freeSlots);

almost 4 years ago · Santiago Trujillo Relatório

0

You want to filter your allSlots array so that it only contains slots not present in existingBookings

const unusedSlots = allSlots.filter((slot) => {
  // See if the slot is booked
  const isBooked = existingBookings.some(
    (booking) => booking.time == slot.time && booking.date == slot.date
  )

  // Only keep free slots
  return !isBooked
});
almost 4 years ago · Santiago Trujillo 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