简体   繁体   English

比较两个 arrays 对象并在两个匹配时删除

[英]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:我正在使用 React,我有两个 arrays 对象:

  • 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.我正在努力寻找一种成功循环遍历allSlots数组并删除existingBookings数组中具有匹配时间的任何方法的方法。

In the example below, there are existing bookings at 10:00am, 10:40am, 11:00am and 11:20am.在下面的示例中,存在上午 10:00、10:40、11:00 和 11:20 的现有预订。

The expected output would only leave 10:20am and 11:40am from the original array.预期的 output 只会离开原始数组的上午 10:20 和上午 11:40。

    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:我最初过滤existingBookings数据以删除任何与所选日期不匹配的数据:

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.我非常感谢您提供的任何见解和帮助。

You want to filter your allSlots array so that it only contains slots not present in existingBookings您想要过滤allSlots数组,以便它只包含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
});

This should work.这应该工作。

  const newAllSlots = allSlots.filter((slot)=>{
    const iSInExistingBookings = existingBookings.find(booking=>booking.time === slots.time && booking.date === slots.date)
    return !iSInExistingBookings
  })

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:这是一个有效的(普通的)JavaScript 片段:

 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);

Since you know what you already have booked with existingBookings , I would suggest looping through that first.由于您知道您已经使用existingBookings预订了哪些内容,我建议您先循环一下。 Then you can recursively filter the allSlots array back into itself by checking if the date matches but the time does not.然后,您可以通过检查日期是否匹配但时间不匹配,以递归方式将allSlots数组过滤回自身。 Once completed, the allSlots array should only have the date and times that haven't been booked.完成后, allSlots数组应仅包含尚未预订的日期和时间。

existingBookings.forEach(booking => {        
    allSlots = allSlots.filter(slot => {
        return booking.date === slot.date && booking.time !== slot.time;
    });
});

Edit: You will have to scope allSlots with let instead of const for this to work.编辑:您必须将 scope allSlotslet而不是const才能正常工作。 You could also assign allSlots to another variable if you want to keep allSlots in tact:如果你想保持allSlots ,你也可以将allSlots分配给另一个变量:

let slots = allSlots;
existingBookings.forEach(booking => {        
    slots = slots.filter(slot => {
        return booking.date === slot.date && booking.time !== slot.time;
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM