Created
April 27, 2017 17:56
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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