extension Array { func dictionarize<Key: Hashable>(by key: (Element) -> Key) -> [Key: Element] { var result: [Key: Element] = [:] for element in self { result[key(element)] = element } return result } }
// [User] to [String: User] users.dictionarize { $0.id }