24.1 Map Objects
Maps are collections of key/value pairs where both the keys and values may be arbitrary
Maps must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements in the collection. The data structure used in this specification is only intended to describe the required observable semantics of Maps. It is not intended to be a viable implementation model.
24.1.1 The Map Constructor
The Map
- is %Map%.
- is the initial value of the
"Map" property of theglobal object . - creates and initializes a new Map when called as a
constructor . - is not intended to be called as a function and will throw an exception when called in that manner.
- may be used as the value in an
extends
clause of a class definition. Subclassconstructors that intend to inherit the specified Map behaviour must include asuper
call to the Mapconstructor to create and initialize the subclass instance with the internal state necessary to support theMap.prototype
built-in methods.
24.1.1.1 Map ( [ iterable ] )
This function performs the following steps when called:
- If NewTarget is
undefined , throw aTypeError exception. - Let map be ?
OrdinaryCreateFromConstructor (NewTarget,"%Map.prototype%" , « [[MapData]] »). - Set map.[[MapData]] to a new empty
List . - If iterable is either
undefined ornull , return map. - Let adder be ?
Get (map,"set" ). - If
IsCallable (adder) isfalse , throw aTypeError exception. - Return ?
AddEntriesFromIterable (map, iterable, adder).
If the parameter iterable is present, it is expected to be an object that implements an
24.1.1.2 AddEntriesFromIterable ( target, iterable, adder )
The abstract operation AddEntriesFromIterable takes arguments target (an Object), iterable (an
- Let iteratorRecord be ?
GetIterator (iterable,sync ). - Repeat,
- Let next be ?
IteratorStepValue (iteratorRecord). - If next is
done , return target. - If next
is not an Object , then- Let error be
ThrowCompletion (a newly createdTypeError object). - Return ?
IteratorClose (iteratorRecord, error).
- Let error be
- Let k be
Completion (Get (next,"0" )). IfAbruptCloseIterator (k, iteratorRecord).- Let v be
Completion (Get (next,"1" )). IfAbruptCloseIterator (v, iteratorRecord).- Let status be
Completion (Call (adder, target, « k, v »)). IfAbruptCloseIterator (status, iteratorRecord).
- Let next be ?
The parameter iterable is expected to be an object that implements an
24.1.2 Properties of the Map Constructor
The Map
- has a [[Prototype]] internal slot whose value is
%Function.prototype% . - has the following properties:
24.1.2.1 Map.groupBy ( items, callbackfn )
callbackfn should be a function that accepts two arguments. groupBy
calls callbackfn once for each element in items, in ascending order, and constructs a new Map. Each value returned by callbackfn is used as a key in the Map. For each such key, the result Map has an entry whose key is that key and whose value is an array containing all the elements for which callbackfn returned that key.
callbackfn is called with two arguments: the value of the element and the index of the element.
The return value of groupBy
is a Map.
This function performs the following steps when called:
24.1.2.2 Map.prototype
The initial value of Map.prototype
is the
This property has the attributes { [[Writable]]:
24.1.2.3 get Map [ @@species ]
Map[@@species]
is an
- Return the
this value.
The value of the
Methods that create derived collection objects should call
24.1.3 Properties of the Map Prototype Object
The Map prototype object:
- is %Map.prototype%.
- has a [[Prototype]] internal slot whose value is
%Object.prototype% . - is an
ordinary object . - does not have a [[MapData]] internal slot.
24.1.3.1 Map.prototype.clear ( )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[MapData]]). - For each
Record { [[Key]], [[Value]] } p of M.[[MapData]], do- Set p.[[Key]] to
empty . - Set p.[[Value]] to
empty .
- Set p.[[Key]] to
- Return
undefined .
24.1.3.2 Map.prototype.constructor
The initial value of Map.prototype.constructor
is
24.1.3.3 Map.prototype.delete ( key )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[MapData]]). - For each
Record { [[Key]], [[Value]] } p of M.[[MapData]], do- If p.[[Key]] is not
empty andSameValueZero (p.[[Key]], key) istrue , then- Set p.[[Key]] to
empty . - Set p.[[Value]] to
empty . - Return
true .
- Set p.[[Key]] to
- If p.[[Key]] is not
- Return
false .
The value
24.1.3.4 Map.prototype.entries ( )
This method performs the following steps when called:
- Let M be the
this value. - Return ?
CreateMapIterator (M,key+value ).
24.1.3.5 Map.prototype.forEach ( callbackfn [ , thisArg ] )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[MapData]]). - If
IsCallable (callbackfn) isfalse , throw aTypeError exception. - Let entries be M.[[MapData]].
- Let numEntries be the number of elements in entries.
- Let index be 0.
- Repeat, while index < numEntries,
- Let e be entries[index].
- Set index to index + 1.
- If e.[[Key]] is not
empty , then- Perform ?
Call (callbackfn, thisArg, « e.[[Value]], e.[[Key]], M »). - NOTE: The number of elements in entries may have increased during execution of callbackfn.
- Set numEntries to the number of elements in entries.
- Perform ?
- Return
undefined .
callbackfn should be a function that accepts three arguments. forEach
calls callbackfn once for each key/value pair present in the Map, in key insertion order. callbackfn is called only for keys of the Map which actually exist; it is not called for keys that have been deleted from the Map.
If a thisArg parameter is provided, it will be used as the
callbackfn is called with three arguments: the value of the item, the key of the item, and the Map being traversed.
forEach
does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn. Each entry of a map's [[MapData]] is only visited once. New keys added after the call to forEach
begins are visited. A key will be revisited if it is deleted after it has been visited and then re-added before the forEach
call completes. Keys that are deleted after the call to forEach
begins and before being visited are not visited unless the key is added again before the forEach
call completes.
24.1.3.6 Map.prototype.get ( key )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[MapData]]). - For each
Record { [[Key]], [[Value]] } p of M.[[MapData]], do- If p.[[Key]] is not
empty andSameValueZero (p.[[Key]], key) istrue , return p.[[Value]].
- If p.[[Key]] is not
- Return
undefined .
24.1.3.7 Map.prototype.has ( key )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[MapData]]). - For each
Record { [[Key]], [[Value]] } p of M.[[MapData]], do- If p.[[Key]] is not
empty andSameValueZero (p.[[Key]], key) istrue , returntrue .
- If p.[[Key]] is not
- Return
false .
24.1.3.8 Map.prototype.keys ( )
This method performs the following steps when called:
- Let M be the
this value. - Return ?
CreateMapIterator (M,key ).
24.1.3.9 Map.prototype.set ( key, value )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[MapData]]). - For each
Record { [[Key]], [[Value]] } p of M.[[MapData]], do- If p.[[Key]] is not
empty andSameValueZero (p.[[Key]], key) istrue , then- Set p.[[Value]] to value.
- Return M.
- If p.[[Key]] is not
- If key is
-0 𝔽, set key to+0 𝔽. - Let p be the
Record { [[Key]]: key, [[Value]]: value }. - Append p to M.[[MapData]].
- Return M.
24.1.3.10 get Map.prototype.size
Map.prototype.size
is an
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[MapData]]). - Let count be 0.
- For each
Record { [[Key]], [[Value]] } p of M.[[MapData]], do- If p.[[Key]] is not
empty , set count to count + 1.
- If p.[[Key]] is not
- Return
𝔽 (count).
24.1.3.11 Map.prototype.values ( )
This method performs the following steps when called:
- Let M be the
this value. - Return ?
CreateMapIterator (M,value ).
24.1.3.12 Map.prototype [ @@iterator ] ( )
The initial value of the
24.1.3.13 Map.prototype [ @@toStringTag ]
The initial value of the
This property has the attributes { [[Writable]]:
24.1.4 Properties of Map Instances
Map instances are
24.1.5 Map Iterator Objects
A Map Iterator is an object, that represents a specific iteration over some specific Map instance object. There is not a named
24.1.5.1 CreateMapIterator ( map, kind )
The abstract operation CreateMapIterator takes arguments map (an
- Perform ?
RequireInternalSlot (map, [[MapData]]). - Let closure be a new
Abstract Closure with no parameters that captures map and kind and performs the following steps when called:- Let entries be map.[[MapData]].
- Let index be 0.
- Let numEntries be the number of elements in entries.
- Repeat, while index < numEntries,
- Let e be entries[index].
- Set index to index + 1.
- If e.[[Key]] is not
empty , then- If kind is
key , then- Let result be e.[[Key]].
- Else if kind is
value , then- Let result be e.[[Value]].
- Else,
Assert : kind iskey+value .- Let result be
CreateArrayFromList (« e.[[Key]], e.[[Value]] »).
- Perform ?
GeneratorYield (CreateIterResultObject (result,false )). - NOTE: The number of elements in entries may have increased while execution of this abstract operation was paused by
Yield . - Set numEntries to the number of elements in entries.
- If kind is
- Return
undefined .
- Return
CreateIteratorFromClosure (closure,"%MapIteratorPrototype%" ,%MapIteratorPrototype% ).
24.1.5.2 The %MapIteratorPrototype% Object
The %MapIteratorPrototype% object:
- has properties that are inherited by all Map Iterator Objects.
- is an
ordinary object . - has a [[Prototype]] internal slot whose value is
%IteratorPrototype% . - has the following properties:
24.1.5.2.1 %MapIteratorPrototype%.next ( )
- Return ?
GeneratorResume (this value,empty ,"%MapIteratorPrototype%" ).
24.1.5.2.2 %MapIteratorPrototype% [ @@toStringTag ]
The initial value of the
This property has the attributes { [[Writable]]: