documentcloud/underscore — pull request #551 by kevinrobinson (2012-04-10 19:08:52+00:00)
Title: Add merge, a la Ruby's Hash#merge

This adds in a _.merge() function that works just like Ruby's Hash#merge (http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-merge). This handles more general cases than the very useful _.extend() and _.defaults().

Given:

var originalRecord = { name: 'Kevin', highestScore: 50 };
var otherRecord = { name: 'Kevin', highestScore: 70 };

When:

_.merge(originalRecord, otherRecord, function(key, original, other) {
if (key === 'highestScore') return Math.max(original, other);
return other;
});

Then:

_.isEqual(originalRecord, { name: 'Kevin', highestScore: 70 }) === true