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

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

本文介绍了如何使用另一个数组过滤当前数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下数据.

[
    {
        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
                    }
                ]
    }
]

我还有另一个用于过滤的数组,格式如下.

I have another array for filter in below format.

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

我想要得到的是获得具有上述类型的房间.

What I want to get is get room which have above types.

我正在尝试下面的方法,但是停留在下面的位置.

I am trying below way, but stuck at below point.

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

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

Could someone point me in right direction?

我的结构如下.

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

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

对于过滤器,我具有以下模型

For filter, I have model as below

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?
}

推荐答案

您可以过滤 Hotel 的数组,以仅保留包含 RoomPrice (其> roomType 属性存在于 RoomFilter 数组中,该数组使用 filter 中的两个嵌套的 contains(where:)调用,其中一个进行搜索 Hotel.prices ,另外一个搜索 roomFilters 以查看两个数组之间是否至少有一个公共元素.

You can filter an array of Hotels 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
})

一些一般性建议:您应该使用单数形式命名类型,因为单个 Hotel 实例代表1个 Hotel ,而不是多个,与 RoomPrice s.将所有属性标记为可选和可变也是没有意义的.除非您有充分的理由将所有内容声明为不可变且可选的.

Some general advice: you should name your types using singular form, since a single Hotel instance represents 1 Hotel, not several ones, same for RoomPrices. 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.

这篇关于如何使用另一个数组过滤当前数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 17:33