1

I have something like this:

var users = [];

Then I use this to add to the list:

users[session] = socket;

Now I need to be able to remove from users where session and socket are the same

How would I do this? (it needs to be in plain javascript, not jquery)

This is for use in socket.IO, if anyone knows what that is.

var users = [];

////////////////USER CONNECTED
io.sockets.on('connection', function (socket) {
    socket.emit('connected');
    socket.on('session', function (session) {

 users[session] = socket;

 socket.emit("session_established");

 //ON USER DISCONNECTION/////
  socket.on('disconnect', function () { 

 //need to remove from the users list here, because they are no longer connected//

 socket.broadcast.emit('disconnect', { data : session});
 }); 

 });
 });
3
  • 3
    You are not working with a JS array as intended. You do, in fact, create a JS array instance with your first line of code, but then you use square-bracket notation to manipulate properties of that array object, rather than add/manipulate items within the array. As a result, you've blended array and object usage. To show you what I mean, try checking users.length after your second line of posted code... Commented Feb 9, 2012 at 20:00
  • I have updated my question, showing what it's being used for. Commented Feb 9, 2012 at 20:04
  • session is just a userID, ie: 1 or it could be 81, or 921 Commented Feb 9, 2012 at 20:06

3 Answers 3

1

Then, splice() method is your friend. Use delete to wipe off indexes. Also, get the id using

for (var i in users){
if i == users[i]
...
}
Sign up to request clarification or add additional context in comments.

1 Comment

You can't use splice() on anything except an array with numerical indexes, which this is probably not, and your loop contradicts that.
1

Careful, when you do this:

var users = [];

users[session] = socket; 

you are ending up with an array that has the property with the name that's stored in the session variable, maybe you're better using an object instead:

var users = {};

users[session] = socket; 

if you do that, then you can use delete to remove the property, like this:

delete users[session];

if you really need to use an array, take a look at this link and, in the method section to push to insert elements in a array and splice to remove them


EDIT

Taking into account the added code in the question, I think you could do the following:

var users = [], indexInArray;

indexInArray = users.push({session: session, socket: socket}); //add an object to the array, and store it's position, you could add whatever you like here
indexInArray--;

//Now you can access your recently added value like: users[indexInArray].session or users[indexInArray].socket

//and to delete it
users.splice(indexInArray , 1);

12 Comments

Why do you need to iterate through properties to delete a property?
Okay, I had once before told that I should use [], when I had used {} before.
@DylanCross maybe you needed to store values with an index. Using [] instead of {} doesn't mean it wont work because everything in javascript are objects, but youre declaring an array and not using it like one
if you could look at my example, (if you haven't already) would you possible be able to give me some assistance with doing this, because doing it this way wouldn't work perfectly as I want it to, because a user could be on two different devices at the same time, and the session would be the same number, so I need to have a list that allows multiple sessions, but go by the socket value, (which is a big list of data), and use that to remove from array and such.
@DylanCross Ok, I'll add what I think it could help you in an edit. Bare in mind that maybe you'll have to adap it a little to your problem
|
0

Try this:

for(var x in users) {
    if(users.hasOwnProperty(x) && x === users[x]) { // Might need ==, depending on your values
        delete users[x];
    }
}

2 Comments

users[x] = undefined is not necessary as the property is deleted on the next line
@AlexanderPavlov: Already removed :) I had originally put it in because I couldn't remember if IE6 had problems with delete. Then I realized that IE6 should die anyway so I don't care.

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.