Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Maps

JS Maps

A Map holds key-value pairs where the keys can be any datatype. A Map remembers the original insertion order of the keys.

The Map Methods

The JS Map has the following methods and properties:

  • new Map(); creates a new Map
  • set(); sets the value for a key in a Map
  • get(); gets the value for a key in a Map
  • delete(); removes a Map element specified by the key
  • has(); returns true if a key exists in a Map
  • forEach(); calls a function for each key/value pair in a Map
  • entries(); returns an iterator with the [key, value] pairs in a Map
  • size; property that returns the number of elements in a Map
The new Map() Method

The JS new Map() method is used to create a JS Map. You can pass variable, values, array, objects etc.

					 
        
          const map1 = new Map();
          const map2 = new Map([
            ["apples", 500],
            ["bananas", 300],
            ["oranges", 200]
          ]);
        
      
The set() Method

The JS set() method is used to add values to the Map. The set() method can also be used to update existing values to the Map.

					 
        
          const fruits = new Map([
            ["apples", 500],
            ["bananas", 300],
            ["oranges", 200]
          ]);

        fruits.set("mangos", 100); // set new values ["mangos", 100]
        fruits.set("apples", 400); // update apples
        
      
The get() Method

The JS get() method is used to get values of a key in a Map.

					 
        
          const fruits = new Map([
            ["apples", 500],
            ["bananas", 300],
            ["oranges", 200]
          ]);

        fruits.get("bananas"); // 300
        
      
The forEach() Method

The JS forEach() method invokes (calls) a function for each key/value pair in a Map.

					 
        
          const fruits = new Map([
            ["apples", 500],
            ["bananas", 300],
            ["oranges", 200]
          ]);

          // List all Elements
          let text = "";
          fruits.forEach (function(value, key) {
            text += key + ' = ' + value + "<br>"
          });
        
      
The entries() Method

The JS entries() method returns an iterator object with the [key, values] in a Map so you can use the Iterator object to access the elements.

					 
        
          const fruits = new Map([
            ["apples", 500],
            ["bananas", 300],
            ["oranges", 200]
          ]);

          fruits.entries() // JS Map Iterator having Map key,value pair

          let text = "";
          for (const x of fruits.entries()) {
            text += x;
          }
        
      
The has() Method

The JS has() method returns true if the Map has the passed key element.

					 
        
          const fruits = new Map([
            ["apples", 500],
            ["bananas", 300],
            ["oranges", 200]
          ]);
          fruits.has("apples"); // true
          fruits.has("mangoes"); // false
        
      
The delete() Method

The JS delete() method deletes the passed key element from the Map.

					 
        
          const fruits = new Map([
            ["apples", 500],
            ["bananas", 300],
            ["oranges", 200]
          ]);
          fruits.delete("apples"); // [["bananas", 300],["oranges", 200]]
        
      
The size Property

The JS size property return the size of the Map.

					 
        
          const fruits = new Map([
            ["apples", 500],
            ["bananas", 300],
            ["oranges", 200]
          ]);
          fruits.sizs; // 3
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved