Apps Script V8 implements a couple of useful new ES6 structures – Maps and Sets. Here’s what they are and how to use them

Maps and Sets: What are they?

These are a collection of like or unlike items. Unlike an array, they are aware of what else is in the map or set (so you can avoid duplicates), and unlike an object, you can use anything as the key – including the item value itself.

The difference between Maps and Sets

Maps

A Map is a collection of key/value pairs, whereas a Set is a collection of items. A Set actually has both values and keys too, but they are the same as each other – in other words, the value of the item is used as its key in a Set, whereas the key and the value can be different in a Map.

Sets

Let’s start with some examples of creating and populating Sets. We’ll use the same data as in previous articles on V8.

creating

An empty set is created like this

adding

the add method takes the item you want to add as an argument

has

the has method checks for the existence of an item

keys and value

The key and the value are the same things in a Set, so values and keys return the same result (unlike Map). That’s why there is no get method for a Set. It’s not required, because you already have to know the value you are looking for as it is the same as key

Adding when value already exists, and size

Re-adding the same value has no effect, and causes no error. You can use the size property to examine how many items are in the Set

Deleting

the delete method

Iterating forEach

Aside from using the keys and values methods, the simplest iteration method is forEach

Converting to an array

A common operation is to turn a Set into an Array

Values are by reference

An object stored in a set is not a deep clone of the object, it’s just a reference to it – so changing the original object, also changes the contents of the set (just as would happen with an item in an Array).

Removing dups

Dups can’t exist in Sets, so they are a great way of removing them. Here are two ways of removing dups from an array. Removing dups from a Set is just a matter of adding the Array to a set.

Anything can be a key/value

Maps

Maps and Sets are very similar, except

  • Maps are key/value pairs – where the key can be different from the value
  • Sets are key/value pairs – but the key and value are the same as each other

This section will be very similar to the section on Sets, as the structures are largely the same

creating

An empty Map is created like this

adding

the set method takes a key, plus the item you want to add as  arguments

has

the has method checks for existence of an item

keys and value

The key and the value can be different in Map items (unlike Set items)

Adding when value already exists, and size

Re-adding the same value has no effect, and causes no error. You can use the size property to examine how many items are in the Map

Deleting

the delete method

Iterating forEach

Aside from using the keys and values methods, the simplest iteration method is forEach

Converting to an array

A common operation is to turn a Map into an Array

Values are by reference

An object stored in a Map is not a deep clone of the object, it’s just a reference to it – so changing the original object, also changes the contents of the Map (just as would happen with an item in an Array).

Removing dups

Dups (items with the same key) can’t exist in Maps, so they are a great way to remove them. Here are two ways of removing dups from an array. Removing dups from a Map is just a matter of adding the Array to a Map – but to add, you must first create a key-value pair array for each item of the Array, as in the example below.

Anything can be a key

get

There is no get with Sets, but it is necessary to find a value by its key with Maps. It’s as you’d expect.

Iterating with entries

Another iteration approach is by extracting the key and value from the entries method. This also works for Sets but is kind of pointless since keys an values are the same.

Summary

Sets and Maps can often be a cleaner way of storing data than using Objects or Arrays, even though at first glance they may seem a little redundant. Give them a try. You’ll like them.

Weakmaps and Symbols are related to this topic, but I’ll cover them in a separate article.