Skip to content

Instantly share code, notes, and snippets.

@sdemjanenko
Created February 28, 2014 21:43
Show Gist options
  • Save sdemjanenko/9280593 to your computer and use it in GitHub Desktop.
Save sdemjanenko/9280593 to your computer and use it in GitHub Desktop.
// maps extend across many objects
_.mapExtend = function(array) {
var sources = slice.call(arguments, 1);
sources.unshift({});
var source = _.extend.apply(this, sources); // combine sources
each(array, function(obj) {
for (var prop in source) {
obj[prop] = source[prop];
}
});
return array;
};
test("mapExtend", function() {
var objects = [{text: 'foo', id: 1}, {text: 'bar', id: 5}];
equal(_.mapExtend(objects, {text: 'mapExtend!'}), [{text: 'mapExtend!', id: 1}, {text: 'mapExtend!', id: 5}], 'mapExtend takes one source');
equal(_.mapExtend(objects, {text: 'mapExtend!'}, {id: 2}), [{text: 'mapExtend!', id: 2}, {text: 'mapExtend!', id: 2}], 'mapExtend takes many sources');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment