Today I Learned

hashrocket A Hashrocket project

Get first value from javascript Map

Map is a new key/value data structure in es6 (and its available in your chrome browser RIGHT NOW). But getting values out of a Map without using a key is complicated a little by having to go through a javascript iterator.

> m = new Map([[1,"a"],[2,"b"]])
Map {1 => "a", 2 => "b"}
> m.values()[0]
> m.values()
MapIterator {"a", "b"}

Its all good though, the next() function of a fresh iterator will always return the first value. Well, not really the first value but a value wrapped in an object with two values, the value you are looking for represented by "value" and "done" which tells you whether the iterator has run out of elements or not.

> m = new Map([[1,"a"],[2,"b"]])
Map {1 => "a", 2 => "b"}
> m.values().next().value
See More #javascript TILs
Looking for help? At Hashrocket, our JavaScript experts launch scalable, performant apps on the Web, Android and iOS. Contact us and find out how we can help you.