Analysez ce code !
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]
const 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-promo", ingredients: mapToppings.get("queen") },
{ name: "royal-promo", ingredients: mapToppings.get("royal") },
];
function intersectionBy(a, b, keyFn) {
const setB = new Set(b.map(keyFn));
return a.filter((item) => setB.has(keyFn(item)));
}
const common = intersectionBy(order1,
order2,
(pizza) => pizza.ingredients);
console.log(common);
// [{ name: "queen", ingredients: [...] }, { name: "royal", ingredients: [...] }]