documentcloud/underscore — Issue #425: feature request: ability to find/filter, but return a new array/object
Author: lookfirst
Created: 2012-01-07 23:33:53+00:00
State: open

First off, thanks so much for underscore and coffeescript.

I apologize in advance if this is already possible, so just close this as me being an idiot. I read through the docs about 20 times, but I don't see this method implemented in underscore and I think it would be useful.

What I'm trying to do is convert a list of objects that looks like this:

```[{'id':2343, 'name': 'Jon'}, ...]```

into this:

```[{'label':'Jon', 'value':2343}]```

BUT, only if name.contains('J')

What I'd like is something like this: ``` _.transform(list, iterator, [context]) ```

```
query = 'J'

newlist = _.transform myList, (val) ->
if val.name.contains(query)
{
label: val.name
value: val.id
}

```

The use case I have for doing this is having the jquery autocomplete plugin work on a response from the Facebook Graph API which is requesting a list of your friends (/me/friends).

Sure, I can implement this on my own easily enough, but it would be nice if it was in underscore cause I think it'd fit in nicely with filter and find.

Thanks for your time and consideration.