I'm trying to do an intersection between two arrays of objects.
const list1 = [
{
name: "skinType",
keys: [
{
id: "oily",
label: "Oily"
},
{
id: "dry",
label: "Dry"
}
]
},
{
name: "finish",
keys: [
{
id: "oily",
label: "Oily"
},
{
id: "dry",
label: "Dry"
},
{
id: "matte",
label: "Matte"
},
{
id: "natural",
label: "Natural"
},
{
id: "radiant",
label: "Radiant / Glow"
}
]
},
{
name: "texture",
keys: [
{
id: "matte",
labal: "Matte"
}
]
}
];
const list2 = [
{
name: "skinType",
keys: [
{
id: "oily",
label: "Oily"
},
{
id: "dry",
label: "Dry"
},
{
id: "gandac",
label: "mazga"
}
]
},
{
name: "finish",
keys: [
{
id: "oily",
label: "Oily"
}
]
}
];
I came up with a solution but it only can do the intersection based on the name key from the object. Now I need now to do the intersection based on the id from the keys array.
const intersection = (list1, list2) => {
return list2.filter(drp => list1.some(rect => rect.name === drp.name));
};
const result = intersection(react, drupal);
Expected result:
[
{
name: "skinType",
keys: [
{
id: "oily",
label: "Oily"
},
{
id: "dry",
label: "Dry"
}
]
},
{
name: "finish",
keys: [
{
id: "oily",
label: "Oily"
}
]
}
]