javascriptfilter

Incorrect operation of the filter function on the data array


I am making a simple application that includes a user search. In general, the search works, but not as it should. When searching, users are divided into two blocks - friends from the user's list and global users. Friends from the friends list should not appear in global users.

My current solution only works if there is one friend in the friends list, but if there are many, they will be displayed in the global users list. I don't understand why this happens.

const searchResult = [
  {
    _id: '682dc4b5c6c0975928d2c5a8',
    nickname: 'Test1'
  },
  {
    _id: '682dc4c3c6c0975928d2c5a9',
    nickname: 'Test2'
  },
  {
    _id: '682a1294943779a5f41e3e26',
    nickname: 'GlobalUser1'
  },
  {
    _id:'682dc4a6c6c0975928d2c5a7',
    nickname: 'Test3'
  },
  {
    _id: '682edc075fa9f243b2cef5d5',
    nickname: 'Test4'
  },
  {
    _id: '682eebee62c0c2de682f0644',
    nickname: 'GlobalUser2'
  },
  { _id: '682edc675fa9f243b2cef5dd', nickname: 'Test5' }
];
const userFriend = [
  {
    _id: '682dc4b5c6c0975928d2c5a8',
    nickname: 'Test1'
  },
  {
    _id: '682dc4c3c6c0975928d2c5a9',
    nickname: 'Test2'
  },
  {
    _id: '682dc4a6c6c0975928d2c5a7',
    nickname: 'Test3'
  },
  {
    _id: '682edc075fa9f243b2cef5d5',
    nickname: 'Test4'
  },
  { _id: '682edc675fa9f243b2cef5dd', nickname: 'Test5' }
]
const searchTest = () => {
   let globalUsers = [];
   let userFriends = [];

   if (userFriend.length === 0) {
      globalUsers = searchResult;
   } else {
     userFriends = searchResult.filter(
       (item) => userFriend.some((friend) => friend._id === item._id
     ))
     if (userFriends.length > 0) {
       globalUsers = searchResult.filter(
         (item) => userFriends.some(
           (friend) => friend._id !== item._id,
              ),
            );
          } else {
            null;
          }
        }
        const foundedList = { globalUsers, userFriends };
        console.log(foundedList);
        }

searchTest();

Solution

  • There is a mistake in the logic here:

                globalUsers = searchResult.filter(
                    (item) => userFriends.some(
                        (friend) => friend._id !== item._id,
                    ),
                );
    

    That some call is always going to return true, as it is quite expected that there is some(!) friend whose id is different.

    What you want is that every friend has a different id from the inspected one:

                globalUsers = searchResult.filter(
                    (item) => userFriends.every(
                        (friend) => friend._id !== item._id,
                    ),
                );
    

    Or, you can express the same logic with: "none of the friends should have the same id":

                globalUsers = searchResult.filter(
                    (item) => !userFriends.some(
                        (friend) => friend._id === item._id,
                    ),
                );