1

The following is used to build up a object array:

var users = {};

var user = {};
user[socket.id] = data.username;

if(users[data.roomname]){

   // Room already exists - check user already exists 

   // if data.username does not value exist is users then:

   users[data.roomname].push(user);
}
else{

   // New room

   users[data.roomname] = [user];
}

Over a few iterations we get something like this:

console.log ( 'Users: ', users );

users  { RoomABC:
   [ { YidwzgUHPHEGkQIPAAAD: 'Mr Chipps' },
     { 'JG-gtBMyPm0C1Hi1AAAF': 'Mr T' },
     { '2JFGMEdPbgjTgLGVAAAH': 'Mr Chipps' }, ] }

The issue is trying to ensure that each username is unique, so Mr Chipps should not be added again if that name already exists.

The examples I have seen Assume the keys are known. I have tried a number of things including some, indexOf but I am not able to get a simple 'does UserX already exist' to work.

The following is the latest block of code I tried to only add the user if not already present in the obj array. This works, but it seems very clunky to me; nested loops to get at the correct level to check the value and set a counter if a match found, then check the counter to decide if a match was found or not:

if(users[data.roomname]){

  // Room already exists - check user already exists 
                
  let found = 0;

  // Nested loop - seems a little clunky but it works

  Object.keys(users[data.roomname]).forEach(key => {

     Object.keys(users[data.roomname][key]).forEach(key2 => {

         if ( users[data.roomname][key][key2] === data.username ) {
            found++;
         }
     });
  });

  if ( found == 0 ) {
     users[data.roomname].push(user);
  }
}

I keep thinking surely there is neat one-liner that can do this check for the existence but I cant get any to work.

4
  • If a user is THAT unique that you do not allow a second user with the same name, then why not use the user name as key instead of socket ID? Commented Apr 29, 2021 at 11:54
  • You can event use a Set and have all sorts of built-in methods Commented Apr 29, 2021 at 11:55
  • @mplungjan I have inherited this code and am slowly trying to iron out some bugs. I think the key is a significant value in other parts of the code. I'm just trying to solve one issue at a at time and the first one is the duplicate users issue. Commented Apr 29, 2021 at 11:57
  • @TenG , instead of unique key, use its value as key, Like User = { [data.username]: [socket.id] } Commented Apr 29, 2021 at 12:16

1 Answer 1

1

You could check the values instead of using the keys and exit early if a name is found

if (users[data.roomname]) {
    if (!Object.values(users[data.roomname]).some(v => Object.values(v).some(n => n === data.username))) {
        users[data.roomname].push(user);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.