What is Map.entries method in JavaScript?
The entries method of the Map object will return an iterator object which contains key-value pair for each element of the map object in the insertion order.
Syntax
mapObj.entries();
The entries method returns an iterator object. This object is also iterable, so we can use the for...of loop with the returned iterator.
Code
let map = new Map();map.set('one', 1);map.set('two', 2);map.set('three', 3);let entries = map.entries();for(let entry of entries){let key = entry[0];let val = entry[1];console.log(key, val);}
Explanation
In the code above:
-
We have created a map object and added some entries to the map.
-
Then, we called the
entriesmethod on the created map objects. -
After this, we looped the returned iterator object from the
entriesmethod and printed each key-value pair.