I have the following code.
const data = [
{ id: 1, serviceId: 1, title: "A" },
{ id: 2, serviceId: 3, title: "T" },
{ id: 3, serviceId: 45, title: "R" },
{ id: 4, serviceId: 55, title: "Q" },
{ id: 5, serviceId: 58, title: "S" },
{ id: 6, serviceId: 63, title: "zx" },
{ id: 7, serviceId: 66, title: "y" }
];
const secondData = [55, 66, 1, 58];
const myArrayFiltered = data.filter((el) => {
return secondData.some((f) => {
return f === el.serviceId;
});
});
return (
<div className="App">
{myArrayFiltered.map((el) => (
<div key={el?.id}>
{el.title}-{el.serviceId}
</div>
))}
</div>
);
I have data
that should be filtered with secondData
array.
The final result should be:
{ id: 2, serviceId: 3, title: "T" },
{ id: 3, serviceId: 45, title: "R" },
{ id: 6, serviceId: 63, title: "zx" },
for filtering, I wrote the following code.
const myArrayFiltered = data.filter((el) => {
return secondData.some((f) => {
return f === el.serviceId;
});
But it's returning the following:
{id: 1, serviceId: 1, title: "A" },
{ id: 4, serviceId: 55, title: "Q" },
{ id: 5, serviceId: 58, title: "S" },
{ id: 7, serviceId: 66, title: "y" }
how can I fix that ?
Thanks.