【问题标题】:How to find matching elements in an array and append them in two new different arrays如何在数组中找到匹配的元素并将它们附加到两个新的不同数组中
【发布时间】:2020-04-15 10:48:02
【问题描述】:

我正在尝试查找匹配的元素并将它们附加到两个不同的arrays 中。数据来自后端,所以我不知道团队是什么,但数据包含所有市场。我想找到匹配的元素,例如获取属于 West hamBurnley 的市场,然后将它们附加到 2 个不同的 arrays 中。

这是一个包含所有市场的示例数组

let marketItems = ["West Ham Total Goals - Over/Under 2.5", "Burnley Total Goals - Over/Under 1.5",
                        "Burnley Total Goals - Over/Under 2.5", "West Ham Total Goals - Over/Under 0.5"]

我想将匹配市场追加到数组中

var homeTeamMarkets: [Market] = []
var awayTeamMarkets: [Market] = []

【问题讨论】:

  • homeTeamMarkets = marketItems.filter{ $0.hasPrefix("West Ham")}?
  • @Larme 这只是来自后端的数据的一个示例,如果没有明确使用团队名称,我不知道我想要实现的团队名称。可能是不同的团队
  • 那么你怎么知道“homeTeam”或“awayTeam”呢?
  • @sk123 发布您的Market 结构。此外,来自支持的数据是否采用以下格式:"West Ham Total Goals - Over/Under 2.5"?这并不容易解析。如果后端提供 JSON,请向我们展示一些示例 JSON。
  • @Rob bad 突出显示的人,否则,快速完成:let teams = Array(Set(marketItems.compactMap({ $0.components(separatedBy: " Total Goals - ").first }))) 了解所有团队,然后根据...过滤?但是结构(即自定义模型)会很好。

标签: ios arrays swift filter foreach


【解决方案1】:

以下将创建此字典:

["Burnley": [1.5, 2.5], "West Ham": [2.5, 0.5]]

您还没有表明有任何方法可以区分哪个是“家”,哪个是“离开”。如果你不能,Dictionary 是你能做的最好的。

public extension Dictionary {
  /// Group key-value pairs by their keys.
  ///
  /// - Parameter pairs: Either `Swift.KeyValuePairs<Key, Self.Value.Element>`
  ///   or a `Sequence` with the same element type as that.
  /// - Returns: `[ KeyValuePairs.Key: [KeyValuePairs.Value] ]`
  init<Value, KeyValuePairs: Sequence>(grouping pairs: KeyValuePairs)
  where
    KeyValuePairs.Element == (key: Key, value: Value),
    Self.Value == [Value]
  {
    self =
      Dictionary<Key, [KeyValuePairs.Element]>(grouping: pairs, by: \.key)
      .mapValues { $0.map(\.value) }
  }

  /// Group key-value pairs by their keys.
  ///
  /// - Parameter pairs: Like `Swift.KeyValuePairs<Key, Self.Value.Element>`,
  ///   but with unlabeled elements.
  /// - Returns: `[ KeyValuePairs.Key: [KeyValuePairs.Value] ]`
  init<Value, KeyValuePairs: Sequence>(grouping pairs: KeyValuePairs)
  where
    KeyValuePairs.Element == (Key, Value),
    Self.Value == [Value]
  {
    self.init( grouping: pairs.map { (key: $0, value: $1) } )
  }
}
import Foundation

extension Market {
  enum DecodingError: Error {
    case invalidFormat(String)
    case notANumber(String)
  }

  static func decode(_ strings: [String]) throws -> [ String: [Double] ] {
    .init(
      grouping: try strings.map {
        let nameAndBet = $0.components(separatedBy: " Total Goals - Over/Under ")

        guard nameAndBet.count == 2
        else { throw DecodingError.invalidFormat($0) }

        let betString = nameAndBet[1]
        guard let bet = Double(betString)
        else { throw Market.DecodingError.notANumber(betString) }

        return (teamName: nameAndBet[0], bet)
      }
    )
  }
}
try Market.decode(marketItems)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 2022-01-11
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多