javascript / beginner
Snippet
Filtering Lists with Array.filter()
The filter method creates a new array with all elements that pass the test implemented by the provided function. It is essential in Next.js for showing specific subsets of data, like search results or active items.
snippet.js
1
2
3
function getActiveUsers(users) {return users.filter(user => user.isActive === true);}
nextjs
Breakdown
1
users.filter(...)
Calls the filter method on the users array to create a subset.
2
user => user.isActive === true
A callback function that returns true only for users who are active.