Map<K, V> class
NOTE: For 2.0 the #constructor arguments have changed. Map now optionally accepts a collection, and only checks types in TypeScript.
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:
var map = new go.Map(); // In TypeScript: new go.Map<string, number>();
map.add("one", 1);
map.add("two", 2);
map.add("three", 3);
// now map.count === 3
// and map.getValue("two") === 2
// and map.contains("zero") === false
You can iterate over the key/value pairs in a Map:
var 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 Set that holds all of the keys of a Map. Iterating over that Set will produce values that are the keys in the Map.
Although not precisely implementing the features and semantics of the EcmaScript 6 Map class, this GoJS Map class has synonyms for the following methods and property:
- get(key): #getValue, but returns null instead of undefined when key is not present
- set(key,val): #add
- has(key): #contains
- delete(key): #remove
- clear(): #clear
- size: #count
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 ES6 iteration, so that functionality has not been made somewhat compatible. These collection classes were defined in GoJS before the ES6 collection classes were proposed.
- Available extensions
- Annotations
-
- @JS()
- @staticInterop
Properties
- count ↔ num
-
Available on Map<
This read-only property is the number of associations in the Map.K, V> , provided by the Map$Typings extensiongetter/setter pair - hashCode → int
-
The hash code for this object.
no setterinherited
-
iterator
↔ IMapIterator<
K, V> -
Available on Map<
Gets an object that you can use for iterating over the key-value pairs of a Map. Typical usage:K, V> , provided by the Map$Typings extensiongetter/setter pair -
iteratorKeys
↔ Iterator<
K> -
Available on Map<
Gets an object that you can use for iterating over the keys of a Map. Typical usage:K, V> , provided by the Map$Typings extensiongetter/setter pair -
iteratorValues
↔ Iterator<
V> -
Available on Map<
Gets an object that you can use for iterating over the values of a Map. Typical usage:K, V> , provided by the Map$Typings extensiongetter/setter pair - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- size ↔ num
-
Available on Map<
This read-only property is the number of associations in the Map.K, V> , provided by the Map$Typings extensiongetter/setter pair
Methods
-
add(
K key, V val) → Map< K, V> -
Available on Map<
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.K, V> , provided by the Map$Typings extension -
addAll(
Object coll) → Map< K, V> -
Available on Map<
Adds all of the key-value pairs of another Map to this Map. If a key is already present in this Map, its value is replaced with the corresponding value from the given map.K, V> , provided by the Map$Typings extension -
all(
bool pred(KeyValuePair< K, V> )) → bool -
Available on Map<
This is true if all invocations of the given predicate on items in the collection are true.K, V> , provided by the Map$Typings extension -
any(
bool pred(KeyValuePair< K, V> )) → bool -
Available on Map<
This is true if any invocation of the given predicate on items in the collection is true.K, V> , provided by the Map$Typings extension -
clear(
) → void -
Available on Map<
Clears the Map, removing all key-value associations. This sets the #count to zero.K, V> , provided by the Map$Typings extension -
contains(
K key) → bool -
Available on Map<
Returns whether the given key is in this Map. @param {K} key The key to look up in the Map. @return {boolean} Whether or not the key is contained within the Map.K, V> , provided by the Map$Typings extension -
copy(
) → Map< K, V> -
Available on Map<
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. @expose @return {Map.<K,V>} The new Map with copies of the same entries.K, V> , provided by the Map$Typings extension -
delete(
K key) → bool -
Available on Map<
Removes a key (if found) from the Map.K, V> , provided by the Map$Typings extension -
each(
void func(KeyValuePair< K, V> )) → Map<K, V> -
Available on Map<
Call the given function on each key/value pair in the collection. @expose @param {function(KeyValuePair.<K,V>)} func The argument to the function will be an object with both "key" and "value" properties. This function must not modify the collection. @return {Map.<K,V>} This Map itself @since 1.4K, V> , provided by the Map$Typings extension -
filter(
bool pred(KeyValuePair< K, V> )) → Map<K, V> -
Available on Map<
Call the given predicate on each key-value pair in the collection and for each pair that it returns true, add the key-value association in a new Map.K, V> , provided by the Map$Typings extension -
first(
) → KeyValuePair< K, V> -
Available on Map<
Returns the first key/value pair in the collection, or null if there is none. @return {KeyValuePair.<K,V>} This returns null if there are no items in the collection. @since 1.4K, V> , provided by the Map$Typings extension -
get(
K key) → V? -
Available on Map<
Returns the value associated with a key. @param {K} key The key to look up in the Map. @return {V|null} The value associated with the given key, or null if not present in the Map.K, V> , provided by the Map$Typings extension -
getValue(
K key) → V? -
Available on Map<
Returns the value associated with a key. @param {K} key The key to look up in the Map. @return {V|null} The value associated with the given key, or null if not present in the Map.K, V> , provided by the Map$Typings extension -
has(
K key) → bool -
Available on Map<
Returns whether the given key is in this Map. @param {K} key The key to look up in the Map. @return {boolean} Whether or not the key is contained within the Map.K, V> , provided by the Map$Typings extension -
map<
S> (S func(KeyValuePair< K, V> )) → Map<K, S> -
Available on Map<
Call the given function on each key-value pair in the collection and associate the key with the result of the function in a new Map.K, V> , provided by the Map$Typings extension -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
remove(
K key) → bool -
Available on Map<
Removes a key (if found) from the Map.K, V> , provided by the Map$Typings extension -
set(
K key, V val) → Map< K, V> -
Available on Map<
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.K, V> , provided by the Map$Typings extension -
toArray(
) → Array< KeyValuePair< K, V> > -
Available on Map<
Produces a JavaScript Array of key/value pair objects from the contents of this Map. @return {Array.<KeyValuePair.<K,V>>} A copy of the Map in Array form, each element being an Object with 'key' and 'value' properties.K, V> , provided by the Map$Typings extension -
toKeySet(
) → Set< K> -
Available on Map<
Produces a Set that provides a read-only view onto the keys of this Map. The collection of keys is not copied. @return {Set.K, V> , provided by the Map$Typings extension -
toString(
) → String -
A string representation of this object.
inherited
-
toString$(
) → String -
Available on Map<
@return {string}K, V> , provided by the Map$Typings extension
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited