Top community members
All Wiki Articles Create Wiki Article

In the past we had a dream,
to have place where we could share IT knowledge,
to ask questions without fear that someone will judge us.

Now we are a group of people who make this dream come true. ❤ 💻

If you think sharing knowledge and helping other is valuable.

join our community - Click here

TypeScript - How to use Map?

0 contributions
2 points

Map is a data structure that allows us to keep key-value pairs.

1. Create Map

let map = new Map<string, number>();

2. Map - set, get, size method example

// create map
let map = new Map<string, number>();

// set entities
map.set("Ann", 25);
map.set("Seth", 32);
map.set("Kate", 27);

// get entities
let value1 = map.get("Ann");
console.log("Ann age: " + value1); // Ann age: 25

let value2 = map.get("Seth");
console.log("Seth age: " + value2); // Seth age: 32

let value3 = map.get("Kate");
console.log("Kate age: " + value3); // Kate age: 27

// siz of map
let size = map.size;
console.log("map size: " + size); // map size: 3


Ann age: 25
Seth age: 32
Kate age: 27
map size: 3

Run this example online:


3. Most important methods of Map:

map.set() - add key-value pair to Map
map.get() - retrieve value by key from Map
map.delete() - delete key-value pair by given key from Map
map.has() - check if there is a value for given key - return true / false
map.size() - size of the map, number of key-value pairs in Map

Note about map.set() method.
In other languages or self implemented data structures to add key-value pair to Map the method is named: put, add or set.


Mozilla docs - Map

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more