Skip to content

Instantly share code, notes, and snippets.

@tifletcher
Created April 27, 2017 17:56
Show Gist options
  • Save tifletcher/470a15dce2511e9261dbf5f325d65321 to your computer and use it in GitHub Desktop.
Save tifletcher/470a15dce2511e9261dbf5f325d65321 to your computer and use it in GitHub Desktop.
Convert an array of similar objects into an object with a common property as key
Array.prototype.toHash = function(key){
let hashMap = {}
this.forEach( (el) => {
hashMap[el[key]] = el
})
return hashMap
}
/*
> var arr = [{id: "a", thing: 123}, {id: "b", thing2: 456}]
> arr.toHash("id")
< Object {a: Object, b: Object}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment