Skip to content

Instantly share code, notes, and snippets.

@kimmobrunfeldt
Created January 9, 2015 14:23
Show Gist options
  • Save kimmobrunfeldt/ca012a2a10a467b3f362 to your computer and use it in GitHub Desktop.
Save kimmobrunfeldt/ca012a2a10a467b3f362 to your computer and use it in GitHub Desktop.
// 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