I have a rooms object, inside that object has many rooms array, and in each room there are users element.
var rooms = {"room1":["user1","user2"],"room2":["user1","user2","user3"]};
How do I write a function that remove a specific user from the rooms array when called?
function leaveRoom(user,room){
if(typeof room != "undefined"){
//remove user from the specific room ONLY if room argument is passed
} else {
//remove user from all rooms
}
}
I have tried to use splice with the index of the element but it doesn't work.
Array.prototype.remove = function(value) {
if (this.indexOf(value)!==-1) {
this.splice(this.indexOf(value), 1);
return true;
} else {
return false;
};
}
var roomName="room1",
userName="user1";
rooms[roomName].remove(userName);
actually I'm not even sure if it is an object or array anymore. Please help thanks