Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
Socket io: Cannot for loop or use array methods on socket.rooms

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
    },
...
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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`...
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda