Version: v0.6.1 - Beta.  We welcome contributors & feedback.  THanks!

Maps

A Map is a collection of key/value pairs.

A Map is like a List, but instead of having a specific order (0,1,2,3), each item corresponds with (or “maps to”) a String key.

Maps are surrounded by curly braces { }. Each key/value pair is joined by a colon :.

// An empty Map
$user = {};

// A one-line Map
$user = { name: 'Taylor', age: 22 };

// A multi-line Map
$user = {
    name: 'Taylor',
    job: 'Technologist',
    location: 'Tennessee',
    age: 22,
};

TipThink of a List as a collection of many things, while a Map usually holds the properties of a single thing. (It’s very common to have a List of Maps, such as a set of rows from a database query.)

JargonIn other languages, Maps are called many different things: associative arrays, dictionaries, hash maps, hashes, or records. They all basically mean the same thing: a set of key/value pairs.

Accessing Map Fields 

Use dot (.) notation to set and get fields that are defined in the Map.

$user = {
    name: 'Taylor',
    age: 22,
};

$user.name;
//= 'Taylor'

$user.email;
//= ✖ ERROR - 'email' is not defined

Dynamic Map Values 

Often, Maps are used to create an index with dynamic key/value pairs.

Use [] notation to safely set and get dynamic values.

$numBooks = {};

$numBooks['Mark Twain'] = 28;
$numBooks['Henry Thoreau'] = 5;

$favoriteAuthor = 'Mark Twain';
$numBooks[$favoriteAuthor];
//= 28

$numBooks['Nelson Nobody'];
//= ''  (a safe falsey value)

Map Methods

Here are some common Map methods.

$colorHex = {
    red:   '#ff1111',
    green: '#11ff11',
    blue:  '#1111FF',
};

$colorHex.keys();
//= ['red', 'green', 'blue']

$colorHex.hasKey('green');
//= true

See Also