Interface Map<K, V>

The Map object holds key-value pairs but doesn't remember the original insertion order of the keys (like JS would). Any value (both objects and primitive values) may be used as either a key or a value. Maps are the best choice for dynamic indexing/newindexing, whereas Objects are better for explicit indexing.

Example

const playerData = new Map<Player, PlayerData>();

function f(plr: Player) {
const data = playerData.get(plr); // `data` could be undefined
if (data) { // check to make sure `data` is defined
print(`${plr.Name} has ${data.NumItems} item${data.NumItems === 1 ? "" : "s"}`);
}
}
// Do not use Maps when you can easily explicitly index from an object:

// TS doesn't assume "x" | "y" are the only possible fields.
// You could manually type this as Map<"x" | "y", number>
const point = new Map([["x", 5], ["y", 10]]);
// this is typed as possibly undefined, because "x" can be deleted
print(point.get("x"));

// Instead use an object
const point = { x: 5, y: 10 };
print(point.y++);
point.z = 15 // error!
interface Map<K, V> {
    clear(this): void;
    delete(this, key): boolean;
    forEach(this, callbackfn): void;
    get(this, key): undefined | V;
    has(this, key): boolean;
    isEmpty(this): boolean;
    set(this, key, value): this;
    size(this): number;
}

Type Parameters

  • K
  • V

Hierarchy (view full)

Methods

  • Deletes all members of the Map

    Parameters

    Returns void

  • Deletes the given key from the Map.

    Returns a boolean indicating whether or not a value was removed.

    Parameters

    Returns boolean

  • Performs the specified action for each (element / pair of elements) in the Map

    Parameters

    • this: ReadonlyMap<K, V>
    • callbackfn: ((value, key, self) => void)

      A function that accepts up to three arguments. forEach calls the callbackfn function one time for each (element / pair of elements) in the array.

        • (value, key, self): void
        • Parameters

          • value: V
          • key: K
          • self: this

          Returns void

    Returns void

  • Returns the value associated with the given key

    Parameters

    Returns undefined | V

  • Returns a boolean for whether the given key exists in the Map

    Parameters

    Returns boolean

  • Associates a key with a value which can be accessed later by Map.get

    Parameters

    Returns this