Horje
What is the use of the Map.prototype.keys method in JavaScript Maps ?

In JavaScript, the Map.prototype.keys method is used to get an iterator object that contains the keys of a Map. The iterator object follows the iterable protocol, allowing you to loop through the keys using methods like next() or using a for...of loop.

Syntax:

myMaps.key();
  • myMap: The Map for which you want to obtain an iterator for its keys.

Example: Below is an example of a key method in a map.

Javascript

let myMap = new Map();
 
myMap.set('key1', 'value1');
myMap.set('key2', 'value2');
myMap.set('key3', 'value3');
 
// Get an iterator for the keys of the Map
let keysIterator = myMap.keys();
 
// Use the iterator to loop through the keys
let nextKey = keysIterator.next();
while (!nextKey.done) {
  console.log(nextKey.value);
  nextKey = keysIterator.next();
}

Output

key1
key2
key3




Reffered: https://www.geeksforgeeks.org


JavaScript

Related
What is the use of the Map.prototype.clear method in JavaScript Maps ? What is the use of the Map.prototype.clear method in JavaScript Maps ?
What is the use of the Get method in Maps in JavaScript ? What is the use of the Get method in Maps in JavaScript ?
Difference Between a Set and an Array Difference Between a Set and an Array
What is the use of the Add method in Sets in JavaScript ? What is the use of the Add method in Sets in JavaScript ?
How to Check if an Element Exists in a Set in JavaScript ? How to Check if an Element Exists in a Set in JavaScript ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13