find the keys of a map in typescript

To find the keys of a Map in TypeScript, you can use the keys() method of the Map object. This method returns an iterable containing the keys of the map, which you can then loop through using a for...of loop or the forEach() method.

Here's an example:

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

// Using a for...of loop
for (const key of myMap.keys()) {
  console.log(key);
}

// Using the forEach() method
myMap.forEach((value, key) => {
  console.log(key);
});
241 chars
15 lines

In this example, we first create a Map object called myMap with two key-value pairs. We then loop through the map's keys using both a for...of loop and the forEach() method, logging the keys to the console.

Note that the keys() method returns an iterable, meaning you can also use methods like Array.from() or the spread operator (...) to convert the iterable to an array if desired.

gistlibby LogSnag