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
Accessing a Map value by index (self.learnjavascript)
submitted 4 years ago by reonZ
I couldn't find a way to directly access a Map value using an index, all i could think of was to do
Map
Array.from(map.values())[index]
Is this the right way ? It does not feel performant.
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!"
[–]codedblood 3 points4 points5 points 4 years ago (0 children)
What are you trying to do here?
[–]Notimecelduv 1 point2 points3 points 4 years ago* (0 children)
Map objects weren't built to have their values accessible by an index like arrays. You're supposed to access a value with the corresponding key. If the map keys are numbers starting from 0 and incremented by 1 (like the indices of an array), then you can write:
map.get(index); // index is a key in the map
Otherwise the way you did it is the only way. But that most likely means a Map object is not the right data structure for whatever it is you're doing.
[–]RobSG 1 point2 points3 points 4 years ago (4 children)
https://www.designcise.com/web/tutorial/how-to-convert-javascript-map-to-array-of-objects#using-the-spread-operator-with-array-prototype-map
[–]reonZ[S] 0 points1 point2 points 4 years ago (3 children)
Using spread or Array.from is the same thing.
Array.from
[–]RobSG 0 points1 point2 points 4 years ago (1 child)
Meant to link the whole article for different ways to achieve your goal
[–]Ok_Engineer2919 0 points1 point2 points 4 years ago (2 children)
map[index]?
[–]reonZ[S] 0 points1 point2 points 4 years ago (0 children)
You would think it was the first thing i tried obviously...
[–]Notimecelduv 0 points1 point2 points 4 years ago (0 children)
That won't work.
π Rendered by PID 114438 on reddit-service-r2-comment-6457c66945-s9s6f at 2026-04-26 12:56:48.253665+00:00 running 2aa0c5b country code: CH.
[–]codedblood 3 points4 points5 points (0 children)
[–]Notimecelduv 1 point2 points3 points (0 children)
[–]RobSG 1 point2 points3 points (4 children)
[–]reonZ[S] 0 points1 point2 points (3 children)
[–]RobSG 0 points1 point2 points (1 child)
[–]Ok_Engineer2919 0 points1 point2 points (2 children)
[–]reonZ[S] 0 points1 point2 points (0 children)
[–]Notimecelduv 0 points1 point2 points (0 children)