Skip to content

Commit d249c51

Browse files
committed
refactor(Link/_voteForLink): Use Array#some
Instead of using Array#map, to build a list of IDs of users who voted, and then use Array#includes to check if the user already voted, we can use Array#some. The main advantage is that when iterating, as soon as the callback returns `true` the iteration stops. See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some Signed-off-by: Adrian Oprea <[email protected]>
1 parent 4311b79 commit d249c51

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/components/Link.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ class Link extends Component {
2121
</div>
2222
)
2323
}
24-
24+
2525
_voteForLink = async () => {
2626
const userId = localStorage.getItem(GC_USER_ID)
27-
const voterIds = this.props.link.votes.map(vote => vote.user.id)
28-
if (voterIds.includes(userId)) {
27+
const userHasAlreadyVoted = this.props.link.votes.some(vote => vote.user.id === userId)
28+
if (userHasAlreadyVoted) {
2929
console.log(`User (${userId}) already voted for this link.`)
3030
return
3131
}

0 commit comments

Comments
 (0)