You can filter an array of Hotel
s to only keep hotels that contain a RoomPrice
whose roomType
property is present in an array of RoomFilter
using two nested contains(where:)
calls inside your filter
, one searching Hotel.prices
and the other one searching roomFilters
to see if there is at least a single common element between the two arrays.
let filteredHotels = hotels.filter({ hotel in
hotel.prices?.contains(where: { room in roomFilters.contains(where: {$0.roomType == room.roomType})}) ?? false
})
Some general advice: you should name your types using singular form, since a single Hotel
instance represents 1 Hotel
, not several ones, same for RoomPrice
s. It also doesn’t make sense to mark all properties as optional and mutable. Declare everything as immutable and optional unless you have a good reason not to do so.
solved how to filter current array using another array