0

In my app i am using a 3 party Library returns the map like this

public sids: Map<SocketId, Set<Room>> = new Map();

and when i access it like this

io.of("/").adapter.sids.forEach(function(value, key) {
    console.log(key + ' = ' + value)
})

the output looks like this..

qZq18yG3TkoBPXNTAAAC = [object Set] 
84q_yQYKB7JjOPAIAAAD = [object Set]

and when i log the whole map i get this..

Map(2) {
  'qZq18yG3TkoBPXNTAAAC' => Set(2) { 'qZq18yG3TkoBPXNTAAAC', 'demo' },
  '84q_yQYKB7JjOPAIAAAD' => Set(2) { '84q_yQYKB7JjOPAIAAAD', 'demo' }
}

so my question is how do i access the values in the [object Set] ? i tried via JSON.stringify but that doesn't return anything.

When using the suggested code like this..

  io.of("/").adapter.sids.forEach((set,key)=> {
        let demo = [...set]
        console.log('Key:', key,' Set to array: ', [...set] , set);
    });

i get the following unexpected

Key: UJ3HwAIsHTgE9qvrAAAB Set to array: [] Set(2) { 'UJ3HwAIsHTgE9qvrAAAB', 'demo' } Key: Kvlt-mtnrE5NxMykAAAD Set to array: [] Set(2) { 'Kvlt-mtnrE5NxMykAAAD', 'demo' }

SamS87
  • 41
  • 1
  • 6
  • Possible dupe: https://stackoverflow.com/questions/35193471/how-to-iterate-over-a-set-in-typescript – SuperStormer May 07 '21 at 21:21
  • 2
    the Set object is an array that removes all duplicated values. If you want to print these values you should print the value.values(). More here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set – savageGoat May 07 '21 at 21:23

2 Answers2

2

You can spread a Set into an array if that helps

const map =new Map([
  ['qZq18yG3TkoBPXNTAAAC', new Set(['qZq18yG3TkoBPXNTAAAC','demo'])],
  ['84q_yQYKB7JjOPAIAAAD', new Set(['84q_yQYKB7JjOPAIAAAD','demo'])]
])

map.forEach((set,key)=> {
  console.log('Key:', key,' Set to array: ', [...set]);
  console.log('Set has value "demo" =', set.has('demo'))
});
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • Thanks that looks pretty good, but i hve one more question, why doe my code print Set(2) when there is 2 items in the object but in your sample its just a plain Set ? Just want to make sure i understand – SamS87 May 07 '21 at 21:49
  • It's just how the console is handling a Set object. It's telling you it is a Set with two entries – charlietfl May 07 '21 at 21:51
  • Ok, just making sure i don't overlook something. In your sample you used the set.has, i tried that with key but got the error key.has is not a function. Final one would be can i apply a filter to the map.for each so it would only return the set where a key = a provided value ? – SamS87 May 07 '21 at 21:59
  • The key isn't the Set, the value is – charlietfl May 07 '21 at 22:00
  • Ok, so it seems i can filter on keys directly ? i tried your code in my app but it does not produce the expected output. i have added code and result to my original question – SamS87 May 07 '21 at 22:15
0

Look at the element definition Set<Room>. The map is storing an object of type Set<Room>. Then in

io.of("/").adapter.sids.forEach(function(value, key) {
    console.log(key + ' = ' + value)
})

value is a Set<Room>. Thats why you cannot add to ' = '.

You can cycle over the Set. And in the set you have a Room object (with their own properties).

Emilio Platzer
  • 2,327
  • 21
  • 29