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. In Next.js, this is often used to show only specific items in a list, like products within a price range.
snippet.js
1
2
3
4
5
6
function isExpensive(price) {return price > 100;}const prices = [50, 150, 200, 80];const expensiveItems = prices.filter(isExpensive);
nextjs
Breakdown
1
prices.filter(isExpensive)
Calls the isExpensive function for every item and keeps only those where it returns true.