const arr1 = [
{
id: "c1",
section: {
name: "emerald",
room: { id: "r1", name: "Room 1" }
}
},
{
id: "c2",
section: {
name: "diamond",
room: { id: "r2", name: "Room 2" }
}
},
{
id: "c3",
section: {
name: "gem",
room: { id: "r3", name: "Room 4" }
}
},
{
id: "c4",
section: {
name: "pearl",
room: { id: "r5", name: "Room 5" }
}
}
];
const arr2 = [
{
name: "diamond",
room: { id: "r2", name: "Room 2" }
},
{
name: "gem",
room: { id: "r3", name: "Room 4" }
}
];
The expected result should show:
[{
id: "c2",
section:{
name: 'diamond',
room: {id: "r2", name: "Room 2"}
}
},{
id: "c3",
section:{
name: 'gem',
room: {id: "r3", name: "Room 4"}
}
}];
I am trying to filter one array to another using the filter method, but it's not working as expected. Here is the code I attempted:
arr1.filter((x: any) => [arr2.room.id].includes(x.section.room.id))
This code is not producing the desired output. My intention is to filter out elements from arr1 that do not exist in arr2 by comparing their room IDs.