I want to write:
socket.on("create-room", (roomID)=> socket.room = roomID)
let userS_selected_room = 'f2eac135-eafd-49e1-adc7-c89351703896';
for(room in socket.rooms){
if(room === userS_selected_room) {do_stuff()}
}
I don't understand these: Map(4), [Set]
console.log(socket.rooms)
Set(4) {
'zBROv1Lug0XhoQxCAAAB',
'room1',
'9e9ecaa6-473a-43a6-9ab7-60ff034ab614',
'1dc1547c-d265-4d5a-bd3f-9a5d37bf883a'
}
console.log(socket)/
...
rooms: Map(4) {
'6Otk--hk5SHOVRcrAAAD' => [Set], //socket id
'room1' => [Set],
'f2eac135-eafd-49e1-adc7-c89351703896' => [Set], //this is room id
'91fdf074-a1e7-493b-97b9-5a6050095697' => [Set] //this is room id
},
...
CodePudding user response:
That output is telling you that socket.rooms
is a Map
object, which maps keys (like '6Otk--hk5SHOVRcrAAAD'
) to Set
objects. You can loop through the map using for-of
(not for-in
):
for (const [roomid, room] of sockets.rooms) {
// ...here, `roomid` will be the ID of the room, and
// `room` will be a `Set`...
}
If you don't need the room IDs, you can use the values
method and loop its result:
for (const room] of sockets.rooms.values()) {
// ...here, `room` will be a `Set`...
}
Or if you have a room ID, get the Set
for that room via get
:
const room = socket.rooms.get(roomID);
You'll have to refer to whatever library you're using to populate socket.rooms
in order to find out what the Set
objects contain, but to loop through the elements in the set, you'd use for-of
again:
for (const whatever of room) {
// ...use `whatever`...
}