@technobuddha > library > JSON > Data Structures
Class: JSONMap<K, V>
Defined in: json-map.ts:26
A Map that allows serializable objects keys.
JSONMap serializes keys using JSON.serialize, enabling the use of complex objects as map keys, similar to how Map allows objects, but with value-based equality rather than reference-based.
Example
const map = new JSONMap<{ id: number }, string>();
map.set({ id: 1 }, "one");
map.get({ id: 1 }); // "one"Remarks
- Keys are serialized using JSON, so only JSON-safe objects should be used as keys.
- Key equality is determined by the serialized JSON string, not by object reference.
- Circular references in keys are not supported.
Type Parameters
| Type Parameter | Description |
|---|---|
K extends JsonObject | The type of the key, which must extend JsonObject. |
V | The type of the value. |
Implements
Map<K,V>
Constructors
Constructor
new JSONMap<K, V>(values?:
| null
| Iterable<[K, V], any, any>): JSONMap<K, V>;Defined in: src/json-map.ts:29
Parameters
| Parameter | Type |
|---|---|
values? | | null | Iterable<[K, V], any, any> |
Returns
JSONMap<K, V>
Properties
| Property | Modifier | Type | Default value | Description | Defined in |
|---|---|---|---|---|---|
[toStringTag] | readonly | "JSONMap" | 'JSONMap' | The string tag used when calling Object.prototype.toString on instances of this class. | src/json-map.ts:40 |
Accessors
size
Get Signature
get size(): number;Defined in: src/json-map.ts:45
Returns the number of elements in the map.
Returns
number
Implementation of
Map.sizeMethods
[iterator]()
iterator: MapIterator<[K, V]>;Defined in: src/json-map.ts:125
Returns an iterator over the key-value pairs in the map.
Returns
MapIterator<[K, V]>
Implementation of
Map.[iterator]clear()
clear(): void;Defined in: src/json-map.ts:52
Removes all key-value pairs from the map.
Returns
void
Implementation of
Map.cleardelete()
delete(value: K): boolean;Defined in: src/json-map.ts:59
Deletes the entry associated with the given key from the map.
Parameters
| Parameter | Type |
|---|---|
value | K |
Returns
boolean
Implementation of
Map.deleteentries()
entries(): MapIterator<[K, V]>;Defined in: src/json-map.ts:66
Returns an iterator over the deserialized key-value pairs in the map.
Returns
MapIterator<[K, V]>
Implementation of
Map.entriesforEach()
forEach(callback: (value: V, key: K, map: JSONMap<K, V>) => void, thisArg?: unknown): void;Defined in: src/json-map.ts:75
Executes a provided function once for each key-value pair in the JSONMap.
Parameters
| Parameter | Type |
|---|---|
callback | (value: V, key: K, map: JSONMap<K, V>) => void |
thisArg? | unknown |
Returns
void
Implementation of
Map.forEachget()
get(key: K): undefined | V;Defined in: src/json-map.ts:87
Retrieves the value associated with the given key, or undefined if the key is not found.
Parameters
| Parameter | Type |
|---|---|
key | K |
Returns
undefined | V
Implementation of
Map.gethas()
has(value: K): boolean;Defined in: src/json-map.ts:94
Determines whether the specified key exists in the map.
Parameters
| Parameter | Type |
|---|---|
value | K |
Returns
boolean
Implementation of
Map.haskeys()
keys(): MapIterator<K>;Defined in: src/json-map.ts:101
Returns an iterator over the deserialized keys of the map.
Returns
MapIterator<K>
Implementation of
Map.keysset()
set(key: K, value: V): this;Defined in: src/json-map.ts:110
Sets the value for the specified key in the map.
Parameters
| Parameter | Type |
|---|---|
key | K |
value | V |
Returns
this
Implementation of
Map.setvalues()
values(): MapIterator<V>;Defined in: src/json-map.ts:118
Returns an iterator over the values in the map.
Returns
MapIterator<V>
Implementation of
Map.values