0

I have already read some questions and answers on SO that are related to what I'm asking, but I don't think they answer exactly my question.

I want to create a new global array if it doesn't exists with a variable name.

My Current Code is

function NewRoom(RoomID, UserID, Usrname)
{
    //Check if RoomID array is already declared or not and create new one if not
    //and add UserID and Usrname in it and then return it

    //My Current Code from a SO answer
    RoomID = ( typeof RoomID != 'undefined' && RoomID instanceof Array ) ? RoomID : [];
    RoomID[UserID] = Usrname;
    return RoomID;
}

var users = ['abc','def','ghi'];
for(var i=0; i<3; i++)
{
    NewRoom('Room2', i, users[i]);
}

var users2 = ['jkl','mno','pqr'];
for(var i=0; i<3; i++)
{
    NewRoom('Room3', i, users2[i]);
}

console.log(users);
console.log(users2);

My Console always show last inserted results like for above users and users2 it is

Object { 2="ghi"};
Object { 2="pqr"};

but this does not work and create new array everytime.

4
  • Can you provide us the tests you made please? Commented Jan 29, 2014 at 13:23
  • Well I have already put my code above what you need more? And result of above is it always create new array if this function NewRoom is called in a loop. Commented Jan 29, 2014 at 13:26
  • 1
    @JohnSmith Every time you describe a problem with "does not work" without further explanation, something is missing. Either you receive an error (and you should past all the output you obtain), or you have an unexpected output (and you should post both the output you get and what you expected). Commented Jan 29, 2014 at 13:27
  • Well @Bakuriu when I call console.log(RoomID) after the loop in which function is called it always give the last stored result in RoomID array. Let me update my code. Commented Jan 29, 2014 at 13:34

4 Answers 4

2

You do NOT want to create global variables. You can add the rooms as indices to an object, and read from the object to get all the users in the room.

If you want to create a 'global' array with the rooms, use the following:

(function () {
    var rooms = {};

    function setRoom(RoomID, UserID, Usrname) {
        if (typeof rooms[RoomID] === 'undefined') {
            rooms[RoomID] = {};
        }
        rooms[RoomID][UserID] = Usrname;
    }

    var users = ['abc','def','ghi'];
    for(var i=0; i<3; i++) {
        setRoom('Room2', i, users[i]);
    }

    var users2 = ['jkl','mno','pqr'];
    for(var i=0; i<3; i++) {
        setRoom('Room3', i, users2[i]);
    }

    console.log(rooms['Room2']);
    console.log(rooms['Room3']);
    console.log(rooms);
}) ();

Small tip: never pollute the global scope, except when some general variable SHOULD be global.

10
  • If declare it globaly like you said as var RoomID then it will be a permanent RoomID not will update it by adding data to it. I want to create a new array with whatever RoomID I provide. Commented Jan 29, 2014 at 13:28
  • OK, you want to be able to add new rooms. See my edit. Commented Jan 29, 2014 at 13:37
  • @Hiddle look I have updated my question for better understanding Commented Jan 29, 2014 at 13:40
  • Thanks Hidde. I now got it working. Hidde can you provide trick if I do not want to create associative array? Actually I didnt want to use associative array as i said i question Commented Jan 29, 2014 at 14:21
  • Why would you not want to create an associative array? Commented Jan 29, 2014 at 14:22
0

What about something like this:

function NewArray (RoomId, UserId, UsrName){
  RoomId = RoomId || [];
  RoomId [UserId] = UsrName;
  return RoomId;
 }
2
  • What are you trying to accomplish? Commented Jan 29, 2014 at 13:35
  • My example is not creating new array if you are passing an array. Commented Jan 29, 2014 at 13:37
0

Your array check is probably wrong. Use this instead:

RoomID = Object.prototype.toString.call(RoomID) === '[object Array]' ? RoomID : [];
2
  • No Use :( it is again creating New array with that RoomID I provide. Commented Jan 29, 2014 at 13:31
  • So, look at @JohnSmith comment. Commented Jan 29, 2014 at 13:35
0

You could do the following:

function NewRoom(RoomID, UserID, Usrname)
{
   if($.isArray(RoomID ))
   {
       RoomID[UserID] = Usrname;
       return RoomID;
   }
   else
   {
       RoomID=[];
       RoomID[UserID] = Usrname;
       return RoomID;
   }

Your Answer

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