Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Item {
- let name: String?
- let itemId: String?
- }
- let dict: [String: [Item]] = ["price": [Item(name: "10-25", itemId: "10-25")],
- "publisher": [Item(name: "ABCD", itemId: "576"),
- Item(name: "DEFG", itemId: "925"),
- Item(name: "HIJK", itemId: "1737")]]
- var keys = ["price", "publisher"]
- let reduced = keys.reduce(into: [String]()) { result, current in
- guard let items = dict[current] else { return }
- let itemsStr = items.compactMap {$0.itemId }.joined(separator: ",")
- result.append("\(current):\(itemsStr)")
- }
- let finalStr = reduced.joined(separator: ";")
- print(finalStr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement