1

I need to create the following data structure:

userid, onlinetime, offlinetime

I need the user id to be repeated in the list.

Something like this:

userid: 1, onlinetime: 11:10:30, offlinetime: 11:18:12
userid: 1, onlinetime: 11:14:14, offlinetime: 11:52:41
userid: 2, onlinetime: 8:08:14, offlinetime: 1:15:00

How can I create this object, or a list?

How can I read this object or list? (I need to compare on-line and off-line time.)

4
  • How is represented the data at first ? You generate it from server-side, you get it from forms elements, or request json... ? Commented Jan 22, 2012 at 13:46
  • Create an object from a javascript function. The function returns the user id, online time, offline time. Commented Jan 22, 2012 at 13:47
  • 1
    From your example, it seems a single user can have multiple online and offline times. Also, it would make more sense to have a connectionTimes array, which will contain online and offline times. This can be a part of an object with a userID property. Commented Jan 22, 2012 at 13:50
  • @Abbas This is a good solution. Can you give me an example please? Commented Jan 22, 2012 at 13:52

1 Answer 1

2

You can create User objects with userId and connectionTimes properties and add them to an array of Users like this:

function createUsers()
{
    var users = [];

    users[0] = {
        userId: 1,
        connectionTimes:
            [
                {onlineTime:"11:10:30", offlineTime:"11:18:12"},
                {onlineTime:"11:14:14", offlineTime:"11:52:41"}
            ]
    }

    users[1] = {
        userId: 2,
        connectionTimes: 
            [
                {onlineTime:"8:08:14", offlineTime:"1:15:00"}
            ]
    }   

    return users;
}

Once you have an array of users, you can iterate through it and find a user by comparing the userId property:

var users = createUsers();

for (var i=0;i<users.length;i++)
{
    if (users[i].userId === 2)
    {
        for (var j=0;j<users[i].connectionTimes.length;j++)
        {
            alert("onlineTime: " + users[i].connectionTimes[j].onlineTime + ", " + "offlineTime: " + users[i].connectionTimes[j].offlineTime);
        }   
    }
}

EDIT

Here's a decomposed version of the code, based on a createUser and getConnectionTime function:

function getConnectionTime(onTime, offTime)
{
    return {onlineTime: onTime, offlineTime: offTime};
}

function createUser(uid, connTimes)
{
    return {userId: uid, connectionTimes: connTimes};
}

var users = [];

var connTimes = [];
connTimes.push(getConnectionTime("11:10:30", "11:18:12"));
connTimes.push(getConnectionTime("11:14:14", "11:52:41"));
users.push(createUser(1, connTimes));

connTimes = [];
connTimes.push(getConnectionTime("8:08:14", "1:15:00"));
users.push(createUser(2, connTimes));

for (var i=0;i<users.length;i++)
{
    if (users[i].userId === 2)
    {
        for (var j=0;j<users[i].connectionTimes.length;j++)
        {
            alert("onlineTime: " + users[i].connectionTimes[j].onlineTime + ", " + "offlineTime: " + users[i].connectionTimes[j].offlineTime);
        }   
    }
}
Sign up to request clarification or add additional context in comments.

10 Comments

When I need to generate userid and times dynamically. How I can read the time from the user with id 2?
This is very good! Could you please show me: How to dynamically generate this object? : users[0] = { userId: 1, connectionTimes: [ {onlineTime:"11:10:30", offlineTime:"11:18:12"}, {onlineTime:"11:14:14", offlineTime:"11:52:41"} ] }
The createUser function cannot have onlinetime and offlinetime parameters since a single user can have more then 1 pair of online and offline time. This is what I think the create user function should look like: function createUser(userid, connectionTimes) { ... }. I will update my answer and break down the object into functions.
users[0].connectionTimes.push(getConnectionTime("00:00:00", "00:00:01"))
I was just giving an example. You will have to search it as I have demonstrated in the code above. The condition would be: if (users[i].userId === 1).
|

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.