Skip to content

Instantly share code, notes, and snippets.

@emlun
Created March 1, 2016 11:20
Show Gist options
  • Save emlun/20b6058a30f286576879 to your computer and use it in GitHub Desktop.
Save emlun/20b6058a30f286576879 to your computer and use it in GitHub Desktop.
var _ = require('underscore');
var list1 = [{ id: 0, a: 'a0', b: 'b0' }, { id: 1, a: 'a1', c: 'c1' }];
var list2 = [{ id: 0, c: 'c0', d: 'd0' }, { id: 1, a: 'A1', b: 'b1' }];
var merged = _(list1).chain()
.concat(list2)
.groupBy('id')
.map(function(group, id) {
return _(group).reduce(function(result, next) {
return _.extend(result, next);
});
})
.value();
console.log(list1);
console.log(list2);
console.log(merged);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment