Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Array.prototype.intersect = function(other, idSelector = obj => obj) {
const otherSet = new Set([...other.map(idSelector)]);
// Reference: http://2ality.com/2015/01/es6-set-operations.html
const intersection = new Set(this.filter(object => otherSet.has(idSelector(object))));
return [...intersection];
};
function intersectDemo(ordersOnHold, domesticOrders, internationalOrders) {
const orderIdSelector = order => order.id;
const usOrdersOnHold = ordersOnHold.intersect(domesticOrders, orderIdSelector);
const internationalOrdersOnHold = ordersOnHold.intersect(internationalOrders, orderIdSelector);
const indentBy = 4;
const dividerCharacter = "*";
printHeaderFooter("US Orders on hold", () => printOrders(usOrdersOnHold, indentBy), indentBy, dividerCharacter);
printHeaderFooter("International Orders on hold", () => printOrders(internationalOrdersOnHold, indentBy), indentBy, dividerCharacter);
}
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.