const arr2 = [1, 2, 3];
const arr1 = [4, 5, 6, 7, 8];
const [small, large] = arr1.length < arr2.length
? [arr1, arr2]
: [arr2, arr1];
console.log(small == arr2); // [1, 2, 3] [4, 5, 6, 7, 8]
function intersectionBy(a, b, keyFn) {
const setB = new Set(b.map(keyFn));
return a.filter((item) => setB.has(keyFn(item)));
}
mapToppings = new Map([
["queen", ["🐷", "🍄", "🍅", "🧀"]],
["cheese", ["🧀", "🍅"]],
["oriental", ["🍅", "🐑", "🍄", "🌶"]],
["royal", ["🍅", "🌵"]],
]);
const order1 = [
{ name: "queen", ingredients: mapToppings.get("queen") },
{ name: "cheese", ingredients: mapToppings.get("cheese") },
{ name: "oriental", ingredients: mapToppings.get("oriental") },
{ name: "royal", ingredients: mapToppings.get("royal") },
];
const order2 = [
{ name: "queen", ingredients: mapToppings.get("queen") },
{ name: "royal", ingredients: mapToppings.get("royal") },
];
const common = intersectionBy(order1, order2, (pizza) => pizza.ingredients);