# Class Map<K, V>

GoJS® Diagramming Components
version 3.0.14
by Northwoods Software®

An unordered iterable collection of key/value pairs that cannot contain two instances of the same key. In TypeScript it is a generic class that enforces at compile-time the type of the key and the type of the associated value.

To create a Map:

      const map = new go.Map(); // In TypeScript: new go.Map<string, number>();
      map.set("one", 1);
      map.set("two", 2);
      map.set("three", 3);
      // now map.size === 3
      // and map.get("two") === 2
      // and map.has("zero") === false

You can iterate over the key/value pairs in a Map:

      const it = aMap.iterator;
      while (it.next()) {
        console.log(it.key + ": " + it.value);
      }

Or:

      aMap.each(kvp => {
        console.log(kvp.key + ": " + kvp.value);
      });

But note that there is no guaranteed ordering amongst the key/value pairs.

Call toKeySet to get a read-only go.Set that holds all of the keys of a Map. Iterating over that Set will produce values that are the keys in the Map.

For compatibility with versions of GoJS before version 3.0, the following synonyms are defined:

- getValue(key): get, but returns null instead of undefined when key is not present
- add(key,val): set
- contains(key): has
- remove(key): delete
- count: size

The constructor now takes an optional Iterable or Array argument that provides the initial entries for the new Map.

Note that GoJS iteration is quite different than ECMAScript iteration, so that functionality has not been made somewhat compatible. These collection classes were defined in GoJS before the ECMAScript collection classes were proposed.

#### Type Parameters

- #### K

- #### V

#####
Index

### Constructors

constructor

### Accessors

count
iterator
iteratorKeys
iteratorValues
size

### Methods

[iterator]
add
addAll
all
any
clear
contains
copy
delete
each
entries
first
forEach
get
getValue
has
keys
remove
set
toArray
toKeySet
values

## Constructors

### constructor

- new Map<K, V>(coll?: Map<K, V> | Iterable<IKeyValuePair<K, V>> | IKeyValuePair<K, V>[]): Map<K, V>

- There are two possible constructors:

  `new go.Map()`, for JavaScript

  `new go.Map<K, V>()` for TypeScript

  In TypeScript, the two optional generic arguments describe the types of keys and the types of values that this Map may hold.

  For example, the expression:

      // TypeScript:
      new go.Map<string, go.Point>()

  produces a Map that has keys that must be strings and whose associated values must be Points.

  #### Type Parameters

  - #### K

  - #### V

  #### Parameters

  - ##### `Optional` coll: Map<K, V> | Iterable<IKeyValuePair<K, V>> | IKeyValuePair<K, V>[]

    an optional collection of IKeyValuePairs to add, or an Array of IKeyValuePairs.

  #### Returns Map<K, V>

## Accessors

### `Readonly` count
: number

- This read-only property is the number of associations in the Map.

### `Readonly` iterator
: IMapIterator<K, V>

- Gets an object that you can use for iterating over the key-value pairs of a Map. Typical usage:

        const it = aMap.iterator;
        while (it.next()) {
          console.log("the key: " + it.key + " has value: " + it.value);
        }

### `Readonly` iteratorKeys
: Iterator<K>

- Gets an object that you can use for iterating over the keys of a Map. Typical usage:

        const it = aMap.iteratorKeys;
        while (it.next()) {
          console.log("key: " + it.value);
        }

### `Readonly` iteratorValues
: Iterator<V>

- Gets an object that you can use for iterating over the values of a Map. Typical usage:

        const it = aMap.iteratorValues;
        while (it.next()) {
          console.log("value: " + it.value);
        }

### `Readonly` size
: number

- This read-only property is the number of associations in the Map.

## Methods

### [iterator]

- [iterator](): IterableIterator<[K, V]>

- This class implements the JavaScript `Symbol.iterator`, and can be used with spread syntax or `for ... of` statements.

  #### Returns IterableIterator<[K, V]>

  `since`

  3.0

### add

- add(key: K, val: V): Map<K, V>

- Adds a key-value association to the Map, or replaces the value associated with the key if the key was already present in the map.

  Be careful not to call this method while iterating over the collection.

  #### Parameters

  - ##### key: K

    The key or index for storing the value in the Map.

  - ##### val: V

    The value to add to the Map, associated with the key.

  #### Returns Map<K, V>

  This modified Map.

### addAll

- addAll(coll: Map<K, V> | Iterable<IKeyValuePair<K, V>> | IKeyValuePair<K, V>[]): Map<K, V>

- Adds all of the key-value pairs to this Map. If a key is already present in this Map, its value is replaced with the corresponding value from the given map.

  Be careful not to call this method while iterating over the collection.

  #### Parameters

  - ##### coll: Map<K, V> | Iterable<IKeyValuePair<K, V>> | IKeyValuePair<K, V>[]

    the collection of IKeyValuePairs to add, or an Array of IKeyValuePairs.

  #### Returns Map<K, V>

  This modified Map.

### `Virtual` all

- all(pred: ((a: IKeyValuePair<K, V>) => boolean)): boolean

- This is true if all invocations of the given predicate on items in the collection are true.

  Call the given predicate on each key/value pair in the collection. As soon as a call returns false, this returns false. Otherwise this returns true. For an empty collection this returns true.

  #### Parameters

  - ##### pred: ((a: IKeyValuePair<K, V>) => boolean)

    The argument to the predicate will be an IKeyValuePair. This function must not have any side-effects.

    - - (a: IKeyValuePair<K, V>): boolean

      - #### Parameters

        - ##### a: IKeyValuePair<K, V>

        #### Returns boolean

  True if all predicate calls are true; false otherwise.

### `Virtual` any

- any(pred: ((a: IKeyValuePair<K, V>) => boolean)): boolean

- This is true if any invocation of the given predicate on items in the collection is true.

  Call the given predicate on each key/value pair in the collection. As soon as a call returns true, this returns true. Otherwise this returns false. For an empty collection this returns false.

  #### Parameters

  - ##### pred: ((a: IKeyValuePair<K, V>) => boolean)

    The argument to the predicate will be an IKeyValuePair. This function must not have any side-effects.

    - - (a: IKeyValuePair<K, V>): boolean

      - #### Parameters

        - ##### a: IKeyValuePair<K, V>

        #### Returns boolean

  True if any predicate call is true; false otherwise.

### clear

- clear(): void

- Clears the Map, removing all key-value associations. This sets the count to zero.

  Be careful not to call this method while iterating over the collection.

  #### Returns void

### contains

- contains(key: K): boolean

- Returns whether the given key is in this Map.

  #### Parameters

  - ##### key: K

    The key to look up in the Map.

  #### Returns boolean

  Whether or not the key is contained within the Map.

### `Virtual` copy

- copy(): Map<K, V>

- Makes a shallow copy of this Map. The keys and their values are not copied, so if they are objects they may continue to be shared with the original Map.

  #### Returns Map<K, V>

  The new Map with copies of the same entries.

### delete

- delete(key: K): boolean

- Removes a key (if found) from the Map.

  Be careful not to call this method while iterating over the collection.

  #### Parameters

  - ##### key: K

    The key to remove.

  #### Returns boolean

  true if the key was found and removed, false otherwise.

### `Virtual` each

- each(func: ((a: IKeyValuePair<K, V>) => void)): Map<K, V>

- Call the given function on each key/value pair in the collection.

  #### Parameters

  - ##### func: ((a: IKeyValuePair<K, V>) => void)

    The argument to the function will be an IKeyValuePair. This function must not modify the collection.

    - - (a: IKeyValuePair<K, V>): void

      - #### Parameters

        - ##### a: IKeyValuePair<K, V>

        #### Returns void

  #### Returns Map<K, V>

  This Map itself

### entries

- entries(): IterableIterator<[K, V]>

- Return a JavaScript iterator that produces the entries ([key, value] pairs) of this Map. This does not produce the old GoJS style Iterator -- use the iterator property if you want that.

  #### Returns IterableIterator<[K, V]>

  `since`

  3.0

### first

- first(): IKeyValuePair<K, V>

- Returns the first IKeyValuePair in the collection, or null if there is none.

  #### Returns IKeyValuePair<K, V>

  This returns null if there are no items in the collection.

### forEach

- forEach(callbackFunc: ((value: V, key: K, map: Map<K, V>) => void), thisArg: any): void

- Call a provided function once per each key/value pair in this Map.

  #### Parameters

  - ##### callbackFunc: ((value: V, key: K, map: Map<K, V>) => void)

    a function to call for each entry of the Map

    - - (value: V, key: K, map: Map<K, V>): void

      - #### Parameters

        - ##### value: V

        - ##### key: K

        - ##### map: Map<K, V>

        #### Returns void

  - ##### thisArg: any

    a value to use as this when executing callbackFunc

  #### Returns void

  `since`

  3.0

### get

- get(key: K): V

- Returns the value associated with a key.

  #### Parameters

  - ##### key: K

    The key to look up in the Map.

  #### Returns V

  The value associated with the given key, or null if not present in the Map.

### getValue

- getValue(key: K): V

- Returns the value associated with a key.

  #### Parameters

  - ##### key: K

    The key to look up in the Map.

  #### Returns V

  The value associated with the given key, or null if not present in the Map.

### has

- has(key: K): boolean

- Returns whether the given key is in this Map.

  #### Parameters

  - ##### key: K

    The key to look up in the Map.

  #### Returns boolean

  Whether or not the key is contained within the Map.

### keys

- keys(): IterableIterator<K>

- Return a JavaScript iterator that produces the keys of this Map. This does not produce the old GoJS style Iterator -- use the iteratorKeys property if you want that.

  #### Returns IterableIterator<K>

  `since`

  3.0

### remove

- remove(key: K): boolean

- Removes a key (if found) from the Map.

  Be careful not to call this method while iterating over the collection.

  #### Parameters

  - ##### key: K

    The key to insert.

  #### Returns boolean

  true if the key was found and removed, false otherwise.

### set

- set(key: K, val: V): Map<K, V>

- Adds a key-value association to the Map, or replaces the value associated with the key if the key was already present in the map.

  Be careful not to call this method while iterating over the collection.

  #### Parameters

  - ##### key: K

    The key or index for storing the value in the Map.

  - ##### val: V

    The value to add to the Map, associated with the key.

  #### Returns Map<K, V>

  This modified Map.

### toArray

- toArray(): IKeyValuePair<K, V>[]

- Produces a JavaScript Array of key/value pair objects from the contents of this Map.

  #### Returns IKeyValuePair<K, V>[]

  A copy of the Map in Array form, each element being an IKeyValuePair

### toKeySet

- toKeySet(): Set<K>

- Produces a go.Set that provides a read-only view onto the keys of this Map. The collection of keys is not copied.

  #### Returns Set<K>

### values

- values(): IterableIterator<V>

- Return a JavaScript iterator that produces the values of this Map. This does not produce the old GoJS style Iterator -- use the iteratorValues property if you want that.

  #### Returns IterableIterator<V>

  `since`

  3.0

Copyright © 1998-2024 by Northwoods Software Corporation.