This article explains how to create and use a Map in Typescript. The methods provided by the Map are described with examples in Typescript. Refer to How to use a Javascript Map on how to create and use a Map in Javascript
The Map data type holds key-value pairs. Objects and primitive data types can be used as keys or values in Map elements. The Map maintains the original insertion order of its elements.
const daysMap = new Map()
daysMap.set("day_one", "Monday")
...
let daysMap: Map
daysMap = new Map()
daysMap.set("day_one", "Monday")
var daysMap : { [key:string]:string; } = {};
daysMap = {"day_one":"Monday","day_two":"Tuesday"}
type daysMapType = Record;
const daysMap: daysMapType = {"day_one":"Monday","day_two":"Tuesday"}
Lets initialize a Map with the days of the week. We will use this Map to perform various operations described below.
const daysMap = new Map();
daysMap.set("day_one", "Monday");
daysMap.set("day_two", "Tuesday");
daysMap.set("day_three", "Wednesday");
daysMap.set("day_four", "Thursday");
daysMap.set("day_five", "Friday");
daysMap.set("day_six", "Saturday");
daysMap.set("day_seven", "Sunday");
The
daysMap.forEach((value,key,map) => {
console.log(key, value)
})
daysMap.forEach((value: string, key: string, map: Map) => {
console.log(key, value, map);
});
To iterate through keys use
To iterate through values use
The
daysMap.forEach(function (value, key, map) {
map.set(key, `Extra fun ${value}`)
});
daysMap.forEach(function (value: string, key: string, map: Map) {
map.set(key, `Extra fun ${value}`)
});
Finds values with keys matching the specified criteria
Key starts with:
Find keys start with the stringdaysMap.forEach(function (value, key) {
if(key.match("^day_o")){
console.log(value)//returns Monday
}
});
daysMap.forEach(function (value: string, key: string) {
if(key.match("^day_o")){
console.log(value)//returns Monday
}
});
Keys contain:
Find keys contain stringdaysMap.forEach(function (value, key) {
if(key.includes("_t")){
console.log(value)//returns Tuesday,Wednesday
}
});
daysMap.forEach(function (value: string, key: string) {
if(key.includes("_t")){
console.log(value)//returns Tuesday,Wednesday
}
});
Keys ending with:
Find keys ending with stringdaysMap.forEach(function (value, key) {
if(key.endsWith("hree")){
console.log(value)//returns Wednesday
}
});
daysMap.forEach(function (value: string, key: string) {
if(key.endsWith("hree")){
console.log(value)//returns Wednesday
}
});
Finds values with keys matching the matching criteria and deletes the element. All the map elements with matching keys are deleted.
Key starts with:
Find keys start with stringdaysMap.forEach(function (value, key,map) {
if(key.match("^day_o")){
map.delete(key)//deletes Monday
}
});
daysMap.forEach(function (value: string, key: string, map: Map) {
if(key.match("^day_o")){
map.delete(key)//deletes Monday
}
});
Keys contain:
Find and delete entries with keys contain stringdaysMap.forEach(function (value, key, map) {
if(key.includes("_t")){
map.delete(key)//deletes Tuesday,Wednesday
}
});
daysMap.forEach(function (value: string, key: string, map: Map) {
if(key.includes("_t")){
map.delete(key)//deletes Tuesday,Wednesday
}
});
Keys ending with:
Find and delete entries with keys ending with stringdaysMap.forEach(function (value, key, map) {
if(key.endsWith("hree")){
map.delete(key)//deletes Wednesday
}
});
daysMap.forEach(function (value: string, key: string, map: Map) {
if(key.endsWith("hree")){
map.delete(key)//deletes Wednesday
}
});
Finds values that match the specified criteria. Console logs all the elements with matching search criteria.
Values start with:
Find values start with the stringdaysMap.forEach(function (value, key) {
if(value.match("^Mon")){
console.log(value)//Monday
}
});
daysMap.forEach(function (value: string, key: string) {
if(value.match("^Mon")){
console.log(value)//Monday
}
});
Values contain:
Find entries with values contain stringdaysMap.forEach(function (value, key) {
if(value.includes("es")){
console.log(value)//Tuesday,Wednesday
}
});
daysMap.forEach(function (value: string, key: string) {
if(value.includes("es")){
console.log(value)//Tuesday,Wednesday
}
});
Values ending with:
Find entries with values ending with stringdaysMap.forEach(function (value, key) {
if(value.endsWith("nday")){
console.log(value)//Monday, Sunday
}
});
daysMap.forEach(function (value: string, key: string) {
if(value.endsWith("nday")){
console.log(value)//Monday, Sunday
}
});
The clear method removes all the entries in a Map and empties it.
The size property returns the size of a map. Note that size is a property, not a method.
const anotherDaysMap = new Map([["1","Monday"],["2","Tuesday"],["3","Wednesday"],["4","Thursday"],["5","Friday"],["6","Saturday"],["7","Sunday"]])
let mergedMap:Map = new Map([...Array.from(daysMap.entries()), ...Array.from(anotherDaysMap.entries())]);
Or
daysMap.forEach((value,key) => {
anotherDaysMap.set(key,value)
})
Note that any elements with duplicate keys will be overridden by the last map in the Map constructor.