Next.js Discord

Discord Forum

map function returning undefined

Answered
Brown bear posted this in #help-forum
Open in Discord
Brown bearOP
I have a map function that checks if event matches, if not, i want it to just not return, but it returns undefined and then i get a stupid array of a lot of undefined. I want it to just return the value if it is correct. Bellow is the code and the array.

const newEventArray = eventArray.map((event, index) => { if (filteredIds.includes(event.event_id)) { return event; } else { return; } }); console.log(newEventArray); }, [selectedFilters]);

[ undefined, {…}, {…}, undefined, {…}, undefined, undefined, undefined, undefined, undefined, … ]
Answered by joulev
Instead of .map, use .filter

eventArray.filter(event => filteredIds.includes(event.event_id))
View full answer

3 Replies