【问题标题】:How to filter a nested array of dictionaries with multiple conditions from another array in Swift如何从Swift中的另一个数组中过滤具有多个条件的嵌套字典数组
【发布时间】:2022-01-03 06:44:20
【问题描述】:

示例 json 数据是这样的:

{
  "variations": [
    {
      "variation_id": 391,
      "name": "Fruit Shake - Chocolate, S",
      "price": 10,
      "attribute": [
        {
          "attribute_key": "pa_flavor",
          "name": "Flavor",
          "option": "Chocolate"
        },
        {
          "attribute_key": "pa_size",
          "name": "Size",
          "option": "S"
        }
      ]
    },
    {
      "variation_id": 385,
      "name": "Fruit Shake - Banana, L",
      "price": 18,
      "attribute": [
        {
          "attribute_key": "pa_flavor",
          "name": "Flavor",
          "option": "Banana"
        },
        {
          "attribute_key": "pa_size",
          "name": "Size",
          "option": "L"
        }
      ]
    },
    {
      "variation_id": 386,
      "name": "Fruit Shake - Banana, M",
      "price": 15,
      "attribute": [
        {
          "attribute_key": "pa_flavor",
          "name": "Flavor",
          "option": "Banana"
        },
        {
          "attribute_key": "pa_size",
          "name": "Size",
          "option": "M"
        }
      ]
    }
  ]
}

我的问题是,获取 2 个或更多属性与字符串数组匹配的变量 ID。

例如,chonedProduct = ["Banana", "L"]

我尝试了过滤和包含,但无法匹配来自 selectedProduct 的其他项目。

如果我添加了下一个条件,则返回 nil

【问题讨论】:

  • 将代码添加为文本而不是图像,并包含您声明的数据结构

标签: arrays swift filtering


【解决方案1】:

你可以试试这个:

let varID = product.variations?.filter { att in
            var matchedAttributes = 0
            for thisAttribute in att.attribute {
                if chosenProduct.contains(where: {$0 == thisAttribute.option}) {
                    matchedAttributes += 1
                }
            }
            if matchedAttributes >= 2 {
                return true
            }
            return false
  }

如果有任何疑问,请告诉我。

【讨论】:

    【解决方案2】:

    你可以试试这个:

    var chosenProduct = ["Banana","L"]
    var expectedIds : [Int] = [Int]()
    
    datas.variations?.map{ val in
        val.attribute.map({ val2 in
            let filtered = val2.enumerated().filter({chosenProduct.contains($0.element.option!)})
            
            if filtered.count == chosenProduct.count{
                expectedIds.append(val.variation_id!)
            }
            
        })
    }
    
    print(expectedIds) // [385]
    

    我将 id 放入数组中,因为如果您想更改 chosenProcudt 示例“香蕉”(计数 1)。此映射必须返回variation_id,如[385,386]

    【讨论】:

      【解决方案3】:

      您可以通过Variation 的方法让事情变得更简单:

      extension Variation {
          func attributesMatching(options: [String]) -> [Attribute] {
              attribute.filter { options.contains($0.option) }
          }
      }
      

      然后,你可以写:

      let filtered = product.variations.filter { $0.attributesMatching(options: chosenProductOptions).count >= 2 }
      print(filtered.map { $0.variationID })
      

      【讨论】:

        猜你喜欢
        • 2021-09-17
        • 2021-04-18
        • 1970-01-01
        • 2019-05-22
        • 2022-01-24
        • 1970-01-01
        • 2021-01-26
        • 2019-03-02
        • 1970-01-01
        相关资源
        最近更新 更多