Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Difference between "intersect" & "except" implementation in JavaScript.
// While "intersect" check if "otherSet" has the value
// "except" does the opposite;
// ⚠️ Notice "!otherSet" in "const difference = new Set(this.filter(object => !otherSet.has(idSelector(object))));"
Array.prototype.intersect = function(other, idSelector = obj => obj) {
...
const intersection = new Set(this.filter(object => otherSet.has(idSelector(object))));
return ...
};
Array.prototype.except = function(other, idSelector = obj => obj) {
...
const difference = new Set(this.filter(object => !otherSet.has(idSelector(object))));
return ...
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.