gpt4 book ai didi

arrays - 如果属性值存在于另一个对象数组中,则快速过滤对象数组

转载 作者:搜寻专家 更新时间:2023-11-01 06:25:56 29 4
gpt4 key购买 nike

我有两个对象数组,如下所示:

MeetingRoomSuggestions :

init(suggestionReason: String, organizerAvailability: String,
startTime: String, endTime: String, dStart: Date, availability: String,
emailAddress: String, displayName: String, roomEmail: String,
occupancy: Int, building: String)

Bookings :

init(startTime: String, endTime: String, dStart: Date, organizer :
String, location : String, subject : String)

我希望能够过滤/排除 MeetingRoomSuggestion如果 dStart 来 self 的数组中的对象属性存在于 Bookings数组。

我的代码:

  let filteredArr = meetingRoomSuggestions.filter { meeting in
return bookingArray!.contains(where: { booking in
return booking.dStart == meeting.dStart
})
}

我还尝试过滤起始字符串 - 这在两者中都是相同的。当我在过滤前打印出两个数组时 - 你可以清楚地看到有一个预订存在相同的 dStart .我怎样才能排除这个?

使用代码过滤并打印出来后:

print("meetings:")
for meeting in self.meetingRoomSuggestions {
print(meeting.roomEmail)
print(meeting.dStart)
print(meeting.startTime)
}
print()
print("bookings:")
for booking in self.bookingArray! {
print(booking.location)
print(booking.dStart!)
print(booking.start)
}

print("filtered array: ", filteredArr)

for items in filteredArr {
print("email: ", items.roomEmail)
print("dstart: ", items.dStart)
}

返回:

    meetings:
FirstFloorTestMeetingRoom1@qubbook.onmicrosoft.com
2019-02-20 15:00:00 +0000
2019-02-20T15:00:00.0000000
GroundFloorTestMeetingRoom1@qubbook.onmicrosoft.com
2019-02-20 15:00:00 +0000
2019-02-20T15:00:00.0000000

bookings:
FirstFloorTestMeetingRoom1@qubbook.onmicrosoft.com
2019-02-20 15:00:00 +0000
2019-02-20T15:00:00.0000000
gfprojectroom1@qubbook.onmicrosoft.com
2019-02-21 10:00:00 +0000
2019-02-21T10:00:00.0000000
gfprojectroom1@qubbook.onmicrosoft.com
2019-02-21 16:00:00 +0000
2019-02-21T16:00:00.0000000

filtered array: [QUBook.MeetingSuggestion, QUBook.MeetingSuggestion]
email: FirstFloorTestMeetingRoom1@qubbook.onmicrosoft.com
dstart: 2019-02-20 15:00:00 +0000
email: GroundFloorTestMeetingRoom1@qubbook.onmicrosoft.com
dstart: 2019-02-20 15:00:00 +0000

由于某种原因,过滤后的数组与原始数组相同meetingRoomSuggestions array - 它不会过滤掉具有相同 dStart 的对象的出现.我怀疑过滤器有问题?我以前能够通过将对象数组与字符串数组等进行比较来过滤对象数组,但不是这样。

最佳答案

您在这里使用了某种反向逻辑。你应该做的是:

let filteredArr = meetingRoomSuggestions.filter { meeting in
return !bookingArray.contains(where: { booking in
return booking.dStart == meeting.dStart
})
}

用简单的英语来说:过滤 session ,留下那些dStart 值等于dStart bookingArray 中任何对象的值。

关于arrays - 如果属性值存在于另一个对象数组中,则快速过滤对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54788590/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com