I have two arrays and I want to filter the second based on what's in the first.
const items = [{itemid: 1, itemname: "hello world"}, {itemid: 2, itemname: "test"}]
const links = [{source: 1, target 2}, {source: 3, target: 4}]
//desired output
filteredLinks = [{source: 1, target: 2}]
In this example, since {source: 3, target: 4} don't match any itemid values from items, I want that object to be filtered out in a new array filteredLinks. I thought I could do this by iterating through items and finding the matches that way, but it isn't returning anything.
let filteredLinks = links.filter((i) =>
i.source.includes(
items.forEach((x) => {
return x.itemid;
})
) ||
i.target.includes(
items.forEach((x) => {
return x.itemid;
})
)
)
Is there any way to achieve this similar to what I'm trying?