Skip to content

Instantly share code, notes, and snippets.

@JoeFerrucci
Created May 2, 2016 21:04
Show Gist options
  • Save JoeFerrucci/82c33b23f59526089038fe99d2f0bdba to your computer and use it in GitHub Desktop.
Save JoeFerrucci/82c33b23f59526089038fe99d2f0bdba to your computer and use it in GitHub Desktop.
Threw this together real quick. I'll refactor for more performant code and structure. Performance profiling coming soon.
import Foundation
extension Array {
func zipmap(key key: String, value: String, container: [AnyObject]) -> [[String:AnyObject]] {
var result = [[String:AnyObject]]()
let count = min(self.count, container.count)
for i in 0..<count {
var mapping = [String:AnyObject]()
let k = self[i] as! String
let v = container[i]
mapping[key] = k
mapping[value] = v
result.append(mapping)
}
return result
}
}
let students = ["Joe", "Charlie", "Benjamin", "Peyton", "Alicia"]
let grades = [91, 82, 97, 99, 100]
let map = students.zipmap(key: "name", value: "grade", container: grades)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment