I’m trying to filter objects inside an array in order to render them in React with Ts.
I think that when I want to filter I am changing the typing and that gives me an error with typescript, but it allows me to render correctly.
However, I am trying to find out if there is a more efficient way to do this in the code. I want to filter to category genres. The idea is with a query of my path or url compare it with an array of numbers that are ids, each array belongs to a collection of objects within an array.
Something like this would be the API data
results = [
{id:1, title"spiderman", genre_ids:[14,23,3]},
{id:2, title:"batman", genre_ids:[14,23,3]},
{id:3, title:"batman 2", genre_ids:[14,23,3]}
]
My code
const df = data?.results.map((item) => {
const db = item.genre_ids.map(
(el) => el.toString() === searchParams.get("genre_ids") && item
);
return db.filter((el) => el !== false && el);
});
if (df === undefined) return;
const db = df.reduce((acc, el) => acc.concat(el), []);
console.log(db);
if (searchParams.get("genre_ids"))
return (
<>
{db.map((item) => (
<div key={item.id}>
{toggle ? <ListData item={item} /> : <GridData item={item} />}
</div>
))}
</>
);
The error says this the property “id” does not exist on type “false | movieApiResult” is not assignable to type “movieResult”. Type boolean is not assignable to type “movieApiResult”
