【问题标题】:Show master array results, based on nested array element search in swift显示主数组结果,基于 swift 中的嵌套数组元素搜索
【发布时间】:2019-04-10 10:21:23
【问题描述】:

我有一组示例数据

public var myResults : [Section] =[:]

public var sectionsData: [Section] = [
     Section(name: "Mac", items: [
        Item(name: "MacBook", detail: "Apple's ultraportable laptop, trading portability for speed and connectivity."),
        Item(name: "MacBook Air", detail: "While the screen could be sharper, the updated 11-inch MacBook Air is a very light ultraportable that offers great performance and battery life for the price."),
        Item(name: "MacBook Pro", detail: "Retina Display The brightest, most colorful Mac notebook display ever. The display in the MacBook Pro is the best ever in a Mac notebook."),

        Item(name: "iMac", detail: "iMac combines enhanced performance with our best ever Retina display for the ultimate desktop experience in two sizes."),

        Item(name: "Mac Pro", detail: "Mac Pro is equipped with pro-level graphics, storage, expansion, processing power, and memory. It's built for creativity on an epic scale."),

        Item(name: "Mac mini", detail: "Mac mini is an affordable powerhouse that packs the entire Mac experience into a 7.7-inch-square frame."),
       Item(name: "OS X El Capitan", detail: "The twelfth major release of OS X (now named macOS)."),

      Item(name: "Accessories", detail: "")
        ],collapsed: false),

Section(name: "iPad",
 items: [
        Item(name: "iPad Pro", detail: "iPad Pro delivers epic power, in 12.9-inch and a new 10.5-inch size."),

        Item(name: "iPad Air 2", detail: "The second-generation iPad Air tablet computer designed, developed, and marketed by Apple Inc."),
        Item(name: "iPad mini 4", detail: "iPad mini 4 puts uncompromising performance and potential in your hand."),

        Item(name: "Accessories", detail: "")
        ] ,collapsed: false),


Section(name: "iPhone", items: [
        Item(name: "iPhone 6s", detail: "The iPhone 6S has a similar design to the 6 but updated hardware, including a strengthened chassis and upgraded system-on-chip, a 12-megapixel camera, improved fingerprint recognition sensor, and LTE Advanced support."),

        Item(name: "iPhone 6", detail: "The iPhone 6 and iPhone 6 Plus are smartphones designed and marketed by Apple Inc."),

        Item(name: "iPhone SE", detail: "The iPhone SE was received positively by critics, who noted its familiar form factor and design, improved hardware over previous 4-inch iPhone models, as well as its overall performance and battery life."),

        Item(name: "Accessories", detail: "")
        ],collapsed: false)
]

如果我根据 Item 数组中的详细信息值进行搜索,例如详细信息包含“iPhone”,如何获取主数组结果以及嵌套值

我的结果应该是

myResults = Section(name: "iPhone", items: [
  Item(name: "iPhone 6s", detail: "The iPhone 6S has a similar design to the 6 but updated hardware, including a strengthened chassis and upgraded system-on-chip, a 12-megapixel camera, improved fingerprint recognition sensor, and LTE Advanced support."),
        Item(name: "iPhone 6", detail: "The iPhone 6 and iPhone 6 Plus are smartphones designed and marketed by Apple Inc."),
        Item(name: "iPhone SE", detail: "The iPhone SE was received positively by critics, who noted its familiar form factor and design, improved hardware over previous 4-inch iPhone models, as well as its overall performance and battery life.")
],collapsed: false)

【问题讨论】:

    标签: ios arrays swift dictionary


    【解决方案1】:

    这应该可以解决问题:

    let text = "The iPhone"
    let filteredSections = sectionsData.compactMap({ (aSection) -> Section? in
        let keptItems = aSection.items.filter({ $0.detail.contains(text) })
        if keptItems.isEmpty {
            return nil
        } else {
            return Section(name: aSection.name, items: keptItems, collapsed: aSection.collapsed)
        }
    })
    

    我们迭代sectionsData。 我们根据要搜索的文本过滤 Section 的项目。 如果没有项目,我们跳过它 (return nil)。 否则,我们创建一个新部分,保留该部分的其他值,但包含过滤后的项目。

    【讨论】:

      【解决方案2】:

      这是我的更新版本,因为我设法正确阅读了说明:)

      let filtered = sectionsData.compactMap { (section) -> Section? in  
      let filteredItems = section.items.filter { item in item.detail.contains("iPhone") } 
          if filteredItems.count > 0 {
              return Section(name: section.name, items: filteredItems, collapsed: section.collapsed) 
          }
          return nil
      }
      

      【讨论】:

      • @JoakimDanielson 不错。我将删除我的 cmets 以避免对未来的读者造成任何混淆。
      猜你喜欢
      • 1970-01-01
      • 2019-05-12
      • 2018-05-06
      • 2017-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-27
      • 2016-10-29
      相关资源
      最近更新 更多