Apps Script Course: Set and Map

In Apps Script, you will mostly use arrays. However, there are other objects that can help you in certain cases to manage sets of values.


Set

A Set object allows storing a set of unique values and can be created like this:

const uniqueValues = new Set();

To add values from the start, enter them in an array:

const uniqueValues = new Set([1, 5, 2, 3, 1, 4, 2, 1, 3]);

console.log(...uniqueValues); // Displays: 1 5 2 3 4

You can see that duplicates were directly removed.

The add method allows adding additional values:

const uniqueValues = new Set([1, 5, 2, 3, 1, 4, 2, 1, 3]);

uniqueValues.add(6);
uniqueValues.add(3);

console.log(...uniqueValues); // Displays: 1 5 2 3 4 6

The has method allows checking if a value is present:

const uniqueValues = new Set([1, 5, 2, 3, 1, 4, 2, 1, 3]);

if (uniqueValues.has(4)) {
  console.log('YES'); // Displayed
}

The delete method allows removing a value:

const uniqueValues = new Set([1, 5, 2, 3, 1, 4, 2, 1, 3]);

uniqueValues.delete(3);

console.log(...uniqueValues); // Displays: 1 5 2 4

The size property allows knowing the number of values:

const uniqueValues = new Set([1, 5, 2, 3, 1, 4, 2, 1, 3]);

console.log(uniqueValues.size); // Displays: 5

The forEach method allows iterating over each value:

const uniqueValues = new Set([1, 5, 2, 3, 1, 4, 2, 1, 3]);

uniqueValues.forEach(value => console.log(value));

If necessary, you can convert the Set object into an array:

const uniqueValues = new Set([1, 5, 2, 3, 1, 4, 2, 1, 3]);
const arrayValues = [...uniqueValues];

console.log(arrayValues); // Displays: [ 1, 5, 2, 3, 4 ]

Therefore, to remove duplicates from an array, you can use this shortcut:

let array = [1, 5, 2, 3, 1, 4, 2, 1, 3];
array = [...new Set(array)];

console.log(array); // Displays: [ 1, 5, 2, 3, 4 ]

Map

A Map object allows associating values with keys and can be created like this:

const data = new Map();

The set method allows adding a key-value pair:

const data = new Map();
data.set('name', 'Example');
data.set('firstName', 'Test');

The get method allows retrieving a value based on its key:

const data = new Map();
data.set('name', 'Example');
data.set('firstName', 'Test');

console.log(data.get('name')); // Displays: Example

The delete method allows removing a value based on its key:

const data = new Map();
data.set('name', 'Example');
data.set('firstName', 'Test');
data.delete('firstName');

The size property allows knowing the number of pairs:

const data = new Map();
data.set('name', 'Example');
data.set('firstName', 'Test');

console.log(data.size); // Displays: 2

The forEach method allows iterating over each pair:

const data = new Map();
data.set('name', 'Example');
data.set('firstName', 'Test');

data.forEach((value, key) => console.log(key, value));

If necessary, you can convert the Map object into an array containing an array for each pair:

const data = new Map();
data.set('name', 'Example');
data.set('firstName', 'Test');

const array = [...data];

console.log(array); // Displays: [ [ 'name', 'Example' ], [ 'firstName', 'Test' ] ]