Skip to content

JavaScript map entries | Method

  • by

JavaScript map entries() method returns an object of a new map iterator. The new iterator object contains the [key, value] pairs for each element in the Map object in insertion order.

mapObj.entries()

JavaScript map entries

Simple example code.

<!DOCTYPE html>
<html>
<body>
  <script>
   const map1 = new Map();

   map1.set('0', 'foo');
   map1.set(1, 'bar');

   const itr = map1.entries();

   console.log(itr.next().value);
   console.log(itr.next().value);

   console.log(itr)

 </script>
</body>
</html>

Output:

JavaScript map entries method

More example

const myMap = new Map();
myMap.set('0', 'foo');
myMap.set(1, 'bar');
myMap.set({}, 'baz');

const mapIter = myMap.entries();

console.log(mapIter.next().value); // ["0", "foo"]
console.log(mapIter.next().value); // [1, "bar"]
console.log(mapIter.next().value); // [Object, "baz"]

Let’s see the same example using for loop.

<script>

   var map = new Map();  
   map.set(1,"jQuery");  
   map.set(2,"AngularJS");  
   map.set(3,"Bootstrap");  

   var itr = map.entries();  

   for(i=0;i<map.size;i++)  
   {  
    console.log(itr.next().value);  
  }    

</script>

Output:

Array [ 1, "jQuery" ]
Array [ 2, "AngularJS" ]
Array [ 3, "Bootstrap" ]

Do comment if you have any doubts or suggestions on this Js map method topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading