add a key-value pair to a map in typescript

You can add a key-value pair to a TypeScript or JavaScript Map using the set method. Here is an example:

index.ts
const myMap = new Map();

// add a key-value pair to the Map
myMap.set("key", "value");

// add another key-value pair to the Map
myMap.set(42, { hello: "world" });

console.log(myMap); // Map(2) { "key" => "value", 42 => { hello: "world" } }
243 chars
10 lines

In the code above, we create a new Map with the new keyword. Then, we use the set method to add two key-value pairs to the Map. The first key is a string and the value is also a string. The second key is a number and the value is an object.

Finally, we log the Map to the console to see the result. The output should be:

index.ts
Map(2) { "key" => "value", 42 => { hello: "world" } }
54 chars
2 lines

gistlibby LogSnag