Created
January 9, 2015 14:23
-
-
Save kimmobrunfeldt/ca012a2a10a467b3f362 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
// Return operations to modify array a to b. | |
// e.g. var a = [{id:1}, {id:2}], b = [{id:2}] | |
// operations(a, b) -> [{operation: ‘remove’, item: {id:1}] | |
function operations(a, b) { | |
var aIds = _.pluck(a, 'id'); | |
var bIds = _.pluck(b, 'id'); | |
var notInBoth = _.xor(aIds, bIds); | |
return _.map(notInBoth, function(id) { | |
var isInA = _.contains(aIds, id); | |
return { | |
operation: isInA ? 'remove' : 'add', | |
item: _.find(a.concat(b), function(item) { return item.id === id }) | |
}; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment