Languages
[Edit]
EN

TypeScript - remove items from Map

0 points
Created by:
Dollie-Rutledge
806

In this article, we would like to show you how to remove items from Map in TypeScript.

Quick solution:

mapName.delete('keyName');

 

Practical example

In this example, we remove Map item by key using delete method.

const myMap = new Map<string, string>([
  ['key1', 'value1'],
  ['key2', 'value2'],
  ['key3', 'value3'],
]);

myMap.delete('key1');

myMap.forEach((item) => console.log(item));

Output:

value2
value3

Remove all items

In this example, we use clear() method to remove all items from the Map.

const myMap = new Map<string, string>([
  ['key1', 'value1'],
  ['key2', 'value2'],
  ['key3', 'value3'],
]);

myMap.clear();

console.log(myMap.size); // 0

Output:

0

Alternative titles

  1. TypeScript - delete items from Map
  2. TypeScript - clear items from Map
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join