[Javascript] convert-object-array-to-hash-map-indexed-by-an-attribute-value-of-the-object
var arr = [ { key : 'foo' , val : 'bar' }, { key : 'hello' , val : 'world' } ]; var result = arr . reduce ( function ( map , obj ) { map [ obj . key ] = obj . val ; return map ; }, {}); console . log ( result ); // { foo: 'bar', hello: 'world' } Note: Array.prototype.reduce() is IE9+, so if you need to support older browsers you will need to polyfill it. from http://stackoverflow.com/questions/26264956/convert-object-array-to-hash-map-indexed-by-an-attribute-value-of-the-object from https://lodash.com/docs#indexBy var keyData = [ { 'dir' : 'left' , 'code' : 97 } , { 'dir' : 'right' , 'code' : 100 } ] ; _. indexBy ( keyData , 'dir' ) ; // → { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } } _. ind...