javascript / beginner
Snippet
Checking for Matches with Array.some()
The .some() method tests whether at least one element in an array passes the implemented test function. It returns a boolean immediately upon finding a match, making it highly efficient for status checks in UI components.
snippet.js
1
2
3
4
5
6
function NotificationBadge(props) {const hasUnread = props.messages.some(function(msg) {return msg.status === 'unread';});return hasUnread ? <span>New!</span> : null;}
nextjs
Breakdown
1
props.messages.some(...)
Starts the search through the array to see if any element meets our criteria.
2
return msg.status === 'unread'
The condition we are checking for; if this is true for even one item, .some() returns true.