Skip to content

Instantly share code, notes, and snippets.

@digitalkaoz
Created February 17, 2016 11:37
Show Gist options
  • Save digitalkaoz/ca14eeb60b665328101b to your computer and use it in GitHub Desktop.
Save digitalkaoz/ca14eeb60b665328101b to your computer and use it in GitHub Desktop.
JS Array diffing with objects that respect duplicates
var _ = require('underscore');
var a = [{type: 'green'}];
var b = [{type: 'green'},{type: 'green'}];
var onlyInA = a.filter((current) => {
return b.filter((current_b) => {
return _.isEqual(current, current_b);
}).length == 0
});
var onlyInB = b.filter((current) => {
return a.filter((current_a) => {
return _.isEqual(current, current_a);
}).length == 0
});
var diff = onlyInA.concat(onlyInB);
console.log(diff);
//expected [{type:'green'}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment