Advertisement
Larme

Untitled

Feb 19th, 2023
994
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.12 KB | None | 0 0
  1.     let allKeys = Set(dict1.keys).union(dict2.keys)
  2.  
  3.     let output = allKeys.reduce(into: [String: [Int: [FinalModel]]]()) { partialResult, currentDateKey in
  4.         let firstModels: [FirstModel]? = dict1[currentDateKey]
  5.         let secondModels: [SecondModel]? = dict2[currentDateKey]
  6.         let allIds: Set<Int> = Set(firstModels?.compactMap { $0.id } ?? []).union( secondModels?.compactMap { $0.id } ?? [])
  7.  
  8.         let models: [Int: [FinalModel]] = allIds.reduce(into: [Int: [FinalModel]]()) { partialModelResult, currentIdKey  in
  9.             let first: [FirstModel] = firstModels?.filter { $0.id == currentIdKey } ?? []
  10.             let second: [SecondModel]? = secondModels?.filter { $0.id == currentIdKey }
  11.             //I supposed there can only be one SecondModel for each id
  12.             let finalModel = FinalModel(first: first.isEmpty ? nil : first, second: second?.first)
  13.             var currentlySaved = partialModelResult[currentIdKey, default: []]
  14.             currentlySaved.append(finalModel)
  15.             partialModelResult[currentIdKey] = currentlySaved
  16.         }
  17.         partialResult[currentDateKey] = models
  18.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement