Skip to content

@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

typescript
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 ParameterDescription
K extends JsonObjectThe type of the key, which must extend JsonObject.
VThe type of the value.

Implements

Constructors

Constructor

ts
new JSONMap<K, V>(values?: 
  | null
| Iterable<[K, V], any, any>): JSONMap<K, V>;

Defined in: src/json-map.ts:29

Parameters

ParameterType
values?| null | Iterable<[K, V], any, any>

Returns

JSONMap<K, V>

Properties

PropertyModifierTypeDefault valueDescriptionDefined 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

ts
get size(): number;

Defined in: src/json-map.ts:45

Returns the number of elements in the map.

Returns

number

Implementation of

ts
Map.size

Methods

[iterator]()

ts
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

ts
Map.[iterator]

clear()

ts
clear(): void;

Defined in: src/json-map.ts:52

Removes all key-value pairs from the map.

Returns

void

Implementation of

ts
Map.clear

delete()

ts
delete(value: K): boolean;

Defined in: src/json-map.ts:59

Deletes the entry associated with the given key from the map.

Parameters

ParameterType
valueK

Returns

boolean

Implementation of

ts
Map.delete

entries()

ts
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

ts
Map.entries

forEach()

ts
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

ParameterType
callback(value: V, key: K, map: JSONMap<K, V>) => void
thisArg?unknown

Returns

void

Implementation of

ts
Map.forEach

get()

ts
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

ParameterType
keyK

Returns

undefined | V

Implementation of

ts
Map.get

has()

ts
has(value: K): boolean;

Defined in: src/json-map.ts:94

Determines whether the specified key exists in the map.

Parameters

ParameterType
valueK

Returns

boolean

Implementation of

ts
Map.has

keys()

ts
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

ts
Map.keys

set()

ts
set(key: K, value: V): this;

Defined in: src/json-map.ts:110

Sets the value for the specified key in the map.

Parameters

ParameterType
keyK
valueV

Returns

this

Implementation of

ts
Map.set

values()

ts
values(): MapIterator<V>;

Defined in: src/json-map.ts:118

Returns an iterator over the values in the map.

Returns

MapIterator<V>

Implementation of

ts
Map.values