代码之家  ›  专栏  ›  技术社区  ›  Fahim Parkar

当我们有内部数组并且需要对内部数组应用过滤器时,如何过滤数组

  •  0
  • Fahim Parkar  · 技术社区  · 7 年前

    假设我有如下数据。

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

    我想要的是用价格过滤酒店。

    比方说,我想筛选出低于范围的酒店。

    价格范围为1231-1233>&燃气轮机;这只会让我回到1号酒店。

    价格范围为1231-1431>&燃气轮机;这将使我返回Hotel 1&酒店2。

    我有相同类型的过滤器,但我只有一个价格,所以我所做的如下。

    finalArray = finalArray.filter() {
                        CGFloat(($0.prices![0].price)!) >= minValue 
                        &&
                        CGFloat(($0.prices![0].price)!) <= maxValue
                 }
    

    然而,现在我有一系列的价格,所以我不知道如何处理这种情况。

    问题就在眼前

    $0.prices![0].price
              ^^^
    

    有人能告诉我如何实现这个过滤器的正确方向吗?

    2 回复  |  直到 7 年前
        1
  •  0
  •   Dávid Pásztor    7 年前

    您可以这样做:

    struct Price {
       let roomType: String
       let price: Double
    }
    
    struct Hotel {
       let hotelName: String
       let hotelType: Int
       let prices: [Price]
    }
    
    let hotel1 = Hotel(hotelName: "Hotel 1", hotelType: 1, prices: [
        Price(roomType: "Single Room", price: 1231),
        Price(roomType: "Twin Room", price: 1232),
        Price(roomType: "Triple Room", price: 1233)
    ])
    
    let hotel2 = Hotel(hotelName: "Hotel 2", hotelType: 1, prices: [
        Price(roomType: "Single Room", price: 1241),
        Price(roomType: "Twin Room", price: 1242),
        Price(roomType: "Triple Room", price: 1243)
    ])
    
    func getHotelsInRange(_ hotels: [Hotel],
                          from min: Double,
                          to max: Double) -> [Hotel] {
        return hotels.filter { h in
            h.prices.contains{ p in
                switch p.price {
                case min...max:
                    return true
                default:
                    return false
                }
            }
        }
    }
    
    let result = getHotelsInRange([hotel1, hotel2], from: min, to: max)
    
        2
  •  1
  •   Andrey Volobuev    7 年前

    您只需检查 prices 与条件不匹配的数组。您可以使用 Array.contains 使用否定谓词,以便 contains 一旦发现错误的值(即检查是否有低于最小值或大于最大值的值),将立即返回。

    finalArray = finalArray.filter() { hotelInfo in
        !hotelInfo.prices!.contains(where: {CGFloat($0) <= minValue}) && !hotelInfo.prices!.contains(where: {CGFloat($0) >= maxValue})
    }