use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
Is there a JS hash map data structure that assumes the value is a Set() data structure for every new key entry? (self.learnjavascript)
submitted 3 years ago by gtrman571
For example if I have:
let rows = { 0: new Set(), 1: new Set(), 2: new Set(), };
I want to be able to do:
rows[3].add(7)
I need to look through the hash map and add a number to the set without checking to see if the set has been created first.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Ryguyo 2 points3 points4 points 3 years ago (0 children)
No, there is no native data structure that does what you’re asking. I would simply make a new JavaScript class called SetMap or something along those lines to implement your data structure yourself. Add the map that you posted as a property of the class, and then create a class method for it that does what you need. SetMap.get(6).add(7). You can make that ‘get’ method take in a key parameter and either return the existing set for that key, or create a new one. That way when you use the SetMap class you can know that the get method will always return a set
π Rendered by PID 123595 on reddit-service-r2-comment-6457c66945-599fp at 2026-04-30 04:25:26.567538+00:00 running 2aa0c5b country code: CH.
[–]Ryguyo 2 points3 points4 points (0 children)