Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am working on a chat and using an array to hold the users. Here is my problem:

User1 joins and is given Index 0 in the array via push. User2 joins and is given Index 1 in the array via push.

User1 disconnects and is removed via splice.

NOW User2 becomes Index 0.

User1 reconnects and is given Index 1 via push.

User2 disconnects, and Index 1 is removed which is now User1.

This is of course causing a problem.

So my question is how can I remove the item from the array without the index of the other elements changing? Am I on the wrong track here?

share|improve this question
    
just set the field to null –  shift66 Jun 30 '13 at 5:55
1  
"This is of course causing a problem." - Why "of course"? This could be perfectly acceptable depending on how your other code uses the array. (That is, your other code could be written in a way that isn't dependent on the array indices.) Having said that, you could use an object instead of an array... –  nnnnnn Jun 30 '13 at 5:56
    
Solved, thank you! –  Cyrus Jun 30 '13 at 6:10

2 Answers 2

up vote 2 down vote accepted

Instead of removing the items from the array with splice(), why not just set the value to null or undefined?

Then when you're adding a new user, you can just scan through the array to find the first available slot.

javascript arrays are simply lists of items - they're not keyed to a specific key like you might be familiar with in PHP. So if you want to keep the same position in the array, you can't remove other items - you need to keep them, and just mark them as empty.


You might scan through something like this:

var users = [];
function addUser(user) {
    var id = users.indexOf(null);
    if (id > -1) {
        // found an empty slot - use that
        users[id] = user;
        return id;
    } else {
        // no empty slots found, add to the end and return the index
        users.push(user);
        return users.length - 1;
    }
}
function removeUser(id) {
    users[id] = null;
}
share|improve this answer
    
Thank you for your reply. I had considered using the delete as it would be better than having the index grow with each new user and having the index grow very large. Could you please explain how to "scan through" and find and then assign it to an array. More precisely how to scan, assign if available, and then if not push to the end? –  Cyrus Jun 30 '13 at 5:58
    
Had to edit my reply, hit enter on accident. :/ –  Cyrus Jun 30 '13 at 6:01
1  
Assuming you "clear" a slot by setting it to null, you can use yourArray.indexOf(null) to get the index of the first available slot. If it returns -1 that means there are no null entries, so then you can .push(). –  nnnnnn Jun 30 '13 at 6:04
    
@nnnnnn Thanks, I had overlooked that. I updated my example to use indexOf. –  jcsanyi Jun 30 '13 at 6:10

Another option is to use a javascript object instead of an array.

Something like this:

var users = {};

users[1] = 'user 1';
users[2] = 'user 2';

delete users[1];
alert(users[2]);        // alerts "user 2"
alert(typeof users[1]); // alerts "undefined"

You lose the array length property though, so you'll have to keep track of your max user number yourself.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.