Advertisement
Larme

Untitled

Feb 18th, 2021
877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.71 KB | None | 0 0
  1. struct Item {
  2.     let name: String?
  3.     let itemId: String?
  4. }
  5.  
  6. let dict: [String: [Item]] = ["price": [Item(name: "10-25", itemId: "10-25")],
  7.                               "publisher": [Item(name: "ABCD", itemId: "576"),
  8.                                             Item(name: "DEFG", itemId: "925"),
  9.                                             Item(name: "HIJK", itemId: "1737")]]
  10.  
  11. var keys = ["price", "publisher"]
  12. let reduced = keys.reduce(into: [String]()) { result, current in
  13.     guard let items = dict[current] else { return }
  14.     let itemsStr = items.compactMap {$0.itemId }.joined(separator: ",")
  15.     result.append("\(current):\(itemsStr)")
  16. }
  17. let finalStr = reduced.joined(separator: ";")
  18. print(finalStr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement