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.