Currently, I am attempting to implement a nested filter on an array of objects. The challenge lies in applying the filter within the object based on a key that contains another array of objects.
Below is the snippet of code:
const items = [
{ name: "123", id: 1, value: true, arr: [{ id: 1 }] },
{ name: "456", id: 2, value: false, arr: [{ id: 2 }] },
{ name: "456", id: 2, value: false, arr: [{ id: 3 }] },
{ name: "456", id: 2, value: false, arr: [{ id: 4 }] },
{ name: "456", id: 2, value: false, arr: [{ id: 5 }] },
{ name: "456", id: 2, value: false, arr: [{ id: 6 }] },
];
const newArray = items.filter((objects) => {
return objects.arr.some((item) => item.id === 2);
});
console.log(newArray);
I am unsure about where to place the return statement as my current implementation seems to only result in an empty array being returned.