I’m trying add users to a group and I’m getting current members of created group in order to compare it to all the users in my user list so I can get the users that does not exist in that group. I’m calling users list and group members list from server and both incoming data are arrays.
for (let i = 0; i < group.members.length; i++) {
const filteredList = userList?.filter(m => m.id !== group.members.at(i).id)
console.log(filteredList);
}
This is not working well because I’m getting one or two existing users.
2
Answers
It seems like the issue lies in your comparison logic within the filter function. Your current logic filters out users whose IDs do not match the ID of the current group member being compared. However, this will exclude users who are not exactly the same as the current group member, even if they are in the group.
Instead, you should check if the current group member exists in the user list,
Change your comparison logic to
filter
out the user list fromm => m.id !== group.members.at(i).id
to!groupMemberIds?.has(user.id)
like below.Refer the below code for sample implementation: