I am trying to check if a user (preferrably the id) is contained in an array of users, i am getting some issues when i try doing it this way
if (
res.data.followers.includes(id=1)
){
setIsFollow(true);
console.log("user exists");
}
This is array
[
{id: 1, username: 'destiny', email: 'desphixs@gmail.com'},
{id: 2, username: 'flourish', email: 'flourish@gmail.com'},
]
i want to check if, for example, user with id 2 exists in the array, then conlog ‘user exists’;.
>Solution :
You can do this. Here filter will return an array containing a single object that matches the id. Then you can check the length of the array, which should be equal to 1.
if(res.data.followers.filter(obj => obj.id === 1).length === 1) {
// logic
}