remove a key-value pair from a map in typescript

To remove a key-value pair from a Map in TypeScript, you can use the delete method of the Map object. Here's an example:

index.ts
// create a Map object
let myMap = new Map<string, number>();
myMap.set("key1", 1);
myMap.set("key2", 2);
myMap.set("key3", 3);

// remove a key-value pair from the map
myMap.delete("key2");

// iterate through the map to confirm deletion
for (let entry of myMap.entries()) {
  console.log(entry[0], entry[1]);
}
313 chars
14 lines

In this example, we create a Map object and populate it with some key-value pairs. To remove a key-value pair, we simply call the delete method of the Map object and pass in the key of the pair we want to remove. In this case, we remove the pair with key "key2".

Finally, we iterate through the Map object to confirm that the key-value pair has been removed.

related categories

gistlibby LogSnag