如何使用另一个数组过滤当前数组

时间:2018-06-02 13:18:19

标签: swift filter swift4

我们说我的数据如下。

[
    {
        hotelName : "Hotel 1",
        hotelType : 1
        prices : 
                [
                    {
                        roomType: "Single Room",
                        price : 1231
                    },
                    {
                        roomType: "Twin Room",
                        price : 1232
                    },
                    {
                        roomType: "Triple Room",
                        price : 1233
                    }
                ]
    },
    {
        hotelName : "Hotel 2",
        hotelType : 2
        prices : 
                [
                    {
                        roomType: "Single Room",
                        price : 1241
                    },
                    {
                        roomType: "Twin Room",
                        price : 1242
                    },
                    {
                        roomType: "Triple Room",
                        price : 1243
                    }
                ]
    }
]

我有以下格式的另一个过滤器数组。

[
    {
        "roomType": "Single Room"
    },
    {
        "roomType": "Twin Room"
    }
]

我想得到的是得到以上类型的房间。

我正在尝试下面的方式,但坚持在下面。

    finalArray = finalArray.filter() {
        hotelInfo in
        hotelInfo.prices!.roomType!==(
            // compare for roomType from another array
        )
    }

有人能指出我正确的方向吗?

我的结构如下所示。

struct Hotels: Encodable, Decodable {
    var hotelName: String?
    var hotelType: Int?
    var prices: [RoomPrices]?
}

struct RoomPrices: Encodable, Decodable {
    var roomType: String?
    var price: Double?
}

对于过滤器,我的模型如下

struct RoomFilter: Decodable {
    var roomType: String?
}

仅限1个字典的价格

[
    {
        hotelName : "Hotel 1",
        hotelType : 1
        prices : 
                    {
                        roomType: "Single Room",
                        price : 1231
                    }
    },
    {
        hotelName : "Hotel 2",
        hotelType : 2
        prices : 

                    {
                        roomType: "Twin Room",
                        price : 1242
                    }

    }
]

更新的结构将是

struct Hotels: Encodable, Decodable {
    var hotelName: String?
    var hotelType: Int?
    var prices: RoomPrices?
}

struct RoomPrices: Encodable, Decodable {
    var roomType: String?
    var price: Double?
}

3 个答案:

答案 0 :(得分:1)

你可以这样做:

let roomTypes = [RoomFilter(roomType: "Twin Room"), RoomFilter(roomType: "Single Room")]

let result = hotels.filter { hotel in
    hotel.prices?.contains { price in
        roomTypes.contains { rt in
            rt.roomType == price.roomType
        }
    } ?? false
}

答案 1 :(得分:1)

如果价格是字典:

let roomTypes = [RoomFilter(roomType: "Twin Room"), RoomFilter(roomType: "Single Room")]

let result = hotels.filter { hotel in
    roomTypes.contains { filterRoomType in
        filterRoomType.roomType == hotel.price?.roomType
    }
}

答案 2 :(得分:0)

您可以过滤Hotel数组,以便仅使用两个嵌套{{RoomPrice来保留包含roomType属性RoomFilter的酒店。 1}}调用contains(where:)内部,一个搜索filter,另一个搜索Hotel.prices以查看两个数组之间是否至少有一个公共元素。

roomFilters

一些一般性建议:您应该使用单数形式命名您的类型,因为单个let filteredHotels = hotels.filter({ hotel in hotel.prices?.contains(where: { room in roomFilters.contains(where: {$0.roomType == room.roomType})}) ?? false }) 实例代表1 Hotel,而不是几个,Hotel相同。将所有属性标记为可选和可变属性也没有意义。声明一切都是不可变的和可选的,除非你有充分的理由不这样做。