get all map keys in javascript

To get all map keys in JavaScript, you can iterate over the map using a for loop and retrieve the keys using the Map.keys() method. Here's an example:

index.tsx
const myMap = new Map();
myMap.set('key1', 'value1');
myMap.set('key2', 'value2');
myMap.set('key3', 'value3');

for (const key of myMap.keys()) {
  console.log(key);
}
169 chars
9 lines

Output:

index.tsx
key1
key2
key3
15 chars
4 lines

In the above example, we first create a new map myMap and add some key-value pairs. Then we loop over the map keys using the myMap.keys() method, which returns an iterable containing all the keys in the map. Finally, we log each key to the console.

Note that the Map.keys() method returns an iterator, which can be used with other iteration constructs like for...of and the spread operator (...).

gistlibby LogSnag