I'm using the twitter v2 API for node, I can pull all users who liked a twitter and use an if in a loop, to check if the user I want is inside the array, but depending on if the tweet has many likes the loop it becomes too long.
I would like to know if there is a way to go straight to the information without needing the loop, if there is any filter or something.
const client = twitterApi({ token, secret })
const usersLiked = await client.v2.tweetLikedBy(target, {
asPaginator: true,
})
for await (const user of usersLiked) {
if (user.id === userIdTwitter) {
await task.related('users').attach([userId])
return response.ok({})
}
}
return response.notFound()
The binary search might help you in this situation. Binary search is a search algorithm that divide the search making it faster witch is better than looking and comparing one by one. An import detail is that this array should be in ascending order before searching. I hope I have been helpful somehow.