documentcloud/underscore — pull request #632 by jthomm (2012-06-08 09:08:28+00:00)
Title: Add an `_.unpluck()` function.

Proposed function performs the inverse of `_.pluck()`, adding a new property to every item in a collection. Useful for enriching datasets. User can either pass an array of new values or a callback that accepts each item and returns the value to be added.

This implementation modifies the original objects inside a `_.map()`, which feels weird. A different one might drop `_.extend()` in favor of `_.clone()`-ing each object before adding the new property.

Example usage:

var data = [
{name: 'Sylvia', birthday: new Date(1984, 1, 14)},
{name: 'Lobo', birthday: new Date(2002, 0, 5)}
];

var today = new Date();
var calculateAge = function(obj) {
return Math.floor((today - obj.birthday)/(1000*60*60*24*365));
};

_.unpluck(data, 'age', calculateAge);

// results in [
// {name: 'Sylvia', birthday: new Date(1984, 1, 14), age: 28},
// {name: 'Lobo', birthday: new Date(2002, 0, 5), age: 10}
// ];