documentcloud/underscore — Issue #422: _.index function
Author: JonRimmer
Created: 2012-01-03 16:10:59+00:00
State: open

Something I've added as a hacky mixin in my project, and was wondering if it was worth considering for underscore proper, is an `index` function that creates an object which maps the values of a particular property to the object. It works similarly to the the `pluck` function, but instead of returning an array containing the property values, it returns an object where the object's properties are the values, and the value of each property is the object from the original array.

```javascript
var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];

_.index(stooges, 'name');

=> { 'moe': {name : 'moe', age : 40},
'larry': {name : 'larry', age : 50},
'curly': {name : 'curly', age : 60} }
```

I've found this to be very useful when dealing with collections of objects that have one or more unique properties which you need to use identify them instead of the object reference, for example when binding to a DOM element's value property, or as a parameter or result value in a web service call.