What You Should Know About ES6 Maps

Just Chris
HackerNoon.com
5 min readFeb 16, 2017

--

JavaScript ES6 introduces a new data structure, called maps. Maps are designed as an alternative to using Object literals for storing key/value pairs that require unique keys, and provide very useful methods for iteration.

Object literals as “Maps”

Nothing is more fundamental in JavaScript than object literals. Creating a map of sorts is as simple as declaring it in code.

This will work in many situations, but there are problems with using object literals as maps. We may have keys that clobber Object prototype methods.

Another drawback of using object literals is all keys can only be strings. This works in many situations, but when attempting to use a primitive value as a string, the system will convert it to a string behind the scenes.

This kind of magic which happens under the hood and without notification to the user can cause unexpected results, for instance, if the provided key is an array.

Another problem with using object literals (pre-ES6), is property/key orders are not guaranteed. Just because you have added the keys in a certain order, does not mean they will remain in that order, when you iterate through the keys.

Objects also lacks a forEach method. If you are used to iterating arrays using .forEach(), objects cannot be iterated in this way.

Maps in ES6

ES6 Maps provide a new object for this purpose. You create a new map by calling new Map([iterable]). Iterable can be an array or any iterable object whose elements are key/value pairs. The provided key/value pairs are added to the new map.

Map Properties and Methods

Map provides a very convienent, .size property to get the size of the map. The size is also very convienently shown in the Chrome Dev console, along with the contents of the map.

map.clear()

Clears the map of all entries.

map.delete(key)

Deletes a key and returns if the delete was successful.

map.get(key), map.has(key)

Getting a key, and finding if a key is present.

map.forEach(fn)

Provides a convienient way to iterate the map. You can get keys, values, and the map itself very easily.

for..of

Another option to iterate the map is to use for..of syntax, which can easily provide access to the keys and values of the map.

m.keys()

Returns a full-blown iterator, so you can iterate the keys one by one, on demand, using .next()

m.values(), m.entries()

The map Values and Entries can be iterated in the same way. Entries will give you an array of the kind [key, value].

Maps give you alot of control over operations that need to be performed on the keys, values, or entries.

2d Arrays to Maps

Maps can take 2d arrays in the constructor. Each array entry should have the format [key, value].

Non-Strings as keys

You can use objects of all kinds as map keys. The system will not automatically convert the keys to strings as it does for object literals. This opens up a wide range of opportunities to do interesting things with maps. For instance, setting the document object as a key.

Maps are an important new feature of ES6, and can be used in a wide variety of use cases for storing key/value pairs.

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.

If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!

--

--

Just Chris
HackerNoon.com

Opinions are my own and not those of Google, Inc.