find the values of a map in typescript

To find the values of a Map in TypeScript, you can use the values() method of the Map object. This method returns a new Iterator object that contains the values for each element in the Map object in insertion order. Here's an example:

index.ts
const myMap = new Map([
  ['key1', 'value1'],
  ['key2', 'value2'],
  ['key3', 'value3']
]);

for (const value of myMap.values()) {
  console.log(value);
}
156 chars
10 lines

In this example, we first create a new Map object with three key-value pairs. We then use a for...of loop to iterate over the values in the Map, logging each value to the console.

Alternatively, you can also use the forEach() method of the Map object to iterate over the values. Here's an example:

index.ts
const myMap = new Map([
  ['key1', 'value1'],
  ['key2', 'value2'],
  ['key3', 'value3']
]);

myMap.forEach((value) => {
  console.log(value);
});
147 chars
10 lines

This code is functionally equivalent to the previous example, but uses the forEach() method to iterate over the values in the Map.

gistlibby LogSnag