Created
March 1, 2016 11:20
-
-
Save emlun/20b6058a30f286576879 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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