Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let allKeys = Set(dict1.keys).union(dict2.keys)
- let output = allKeys.reduce(into: [String: [Int: [FinalModel]]]()) { partialResult, currentDateKey in
- let firstModels: [FirstModel]? = dict1[currentDateKey]
- let secondModels: [SecondModel]? = dict2[currentDateKey]
- let allIds: Set<Int> = Set(firstModels?.compactMap { $0.id } ?? []).union( secondModels?.compactMap { $0.id } ?? [])
- let models: [Int: [FinalModel]] = allIds.reduce(into: [Int: [FinalModel]]()) { partialModelResult, currentIdKey in
- let first: [FirstModel] = firstModels?.filter { $0.id == currentIdKey } ?? []
- let second: [SecondModel]? = secondModels?.filter { $0.id == currentIdKey }
- //I supposed there can only be one SecondModel for each id
- let finalModel = FinalModel(first: first.isEmpty ? nil : first, second: second?.first)
- var currentlySaved = partialModelResult[currentIdKey, default: []]
- currentlySaved.append(finalModel)
- partialModelResult[currentIdKey] = currentlySaved
- }
- partialResult[currentDateKey] = models
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement