0

Im trying to create a structure with Javascript as follows:

var users = {
    user.id: {
      session.id1: session.id1,
      session.id2: session.id2,
      session.id3: session.id3
    },
    user.id2: {
      session.id1: session.id1,
      session.id2: session.id2,
      session.id3: session.id3
    },
  };

What i need: add new sessions and remove them, removing okay, but how should i define object and how can i push new sessions to user obejct? That's why key is equal to value.

5
  • user.id cannot be used due to the .. "user.id" would work though, wrap it up in quotes. Commented Jun 12, 2015 at 10:12
  • or just use user{ id: { id1: ... , id2: ... } } and id2:{ id1:..., id2: ... } }. You can then adress them as user.id.id1 or user.id2.id1 Commented Jun 12, 2015 at 10:18
  • I think its better to create a class and then store this object in a users var Commented Jun 12, 2015 at 10:20
  • and push() is not available with objects, only with arrays Commented Jun 12, 2015 at 10:20
  • wrapping "user.id" in quotes don't take it as value from "user" object and displays it as text. Commented Jun 12, 2015 at 10:34

3 Answers 3

3

If you want to use session.id1 instead of something like sessionId1 :

Assign value:

users['user.id'].['session.id1'] = value;

Create object:

var users = {
    'user.id': {
      'session.id1': session.id1,
      'session.id2': session.id2,
      'session.id3': session.id3
    },
    'user.id2': {
      'session.id1': session.id1,
      'session.id2': session.id2,
      'session.id3': session.id3
    },
  };

But I don't recommend it. If you are the only one who is gonna work with this code, it's ok.

Sign up to request clarification or add additional context in comments.

1 Comment

session.id comes from socket.io in nodejs, also from object. And i would like to use the value in it as my key and also as a value.
0

You can first create an empty object and fill it as and when the data comes like

users[user.id] = {};

For an example:

var users = {};
var user = {id : 1}; //Data received(Just an example)
users[user.id] = {};
var session = {id1 : 1.1}; //Data received
users[user.id][session.id1] = session.id1;
console.log(JSON.stringify(users));

1 Comment

The code im using: if(!users[user.user_id]) { users[user.user_id] = {}; } users[user.user_id][socket.id] = socket.id; Thanks! You helped a lot!
0

How about refactoring the user object to store sessions as an array and push, pop and slice them as required.

var users = [
   {
      id:'userid',
      sessions: [
          {
              id: 'sessionid',
              sessiondata: something
          },
          {
              id: 'sessionid',
              sessiondata: something
          }
      ]
 }];

This way to can just use normal array operators on the session array for each user.

1 Comment

What do you mean? Can you please provide some code? Thanks!

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.