Google Apps Script:
Using associative arrays

How to:

In Google Apps Script, you create and manipulate associative arrays (objects) using braces {}, defining key-value pairs within. Keys are unique identifiers, and values can be anything from strings and numbers to more complex objects or functions. Here’s a basic example:

function createAssociativeArray() {
  var user = {
    name: "John Doe",
    age: 30,
    email: "[email protected]"
  };

  // Accessing values
  Logger.log(user.name); // Outputs: John Doe
  Logger.log(user["email"]); // Outputs: [email protected]

  // Adding new key-value pairs
  user.title = "Software Developer";
  user["country"] = "USA";

  Logger.log(user.title); // Outputs: Software Developer

  // Iterating over key-value pairs
  for (var key in user) {
    Logger.log(key + ': ' + user[key]);
  }
}

Sample output for the iteration part might look like this:

name: John Doe
age: 30
email: [email protected]
title: Software Developer
country: USA

Note how you can use both dot notation and bracket notation for accessing and setting properties. Bracket notation is particularly useful when working with keys that are dynamically determined or include characters not permissible in identifiers.

Deep Dive

Associative arrays in form of objects have been a cornerstone of JavaScript, and by extension Google Apps Script, reflecting its prototype-based inheritance mechanism. Unlike languages with traditional associative arrays or dictionaries (e.g., Python’s dict), Google Apps Script objects provide a flexible and powerful means to structure data, benefiting from JavaScript’s dynamic nature.

It’s important to note, however, that the ECMAScript 2015 specification introduced Map and Set objects, offering a more straightforward associative collection handling with certain benefits over objects, such as maintaining insertion order and better performance for large datasets. While Google Apps Script supports these as well, the choice between using objects or the newer Map/Set structures depends on specific needs and performance considerations. For most associative array tasks, traditional object-based implementations provide a familiar and versatile approach, but examining newer alternatives is advisable as your script’s complexity grows.