Map - JavaScript

Map

Description :

Map objects are collections of key-value pairs. A key in the map may only occur once. It is unique in the Map's collections. A Map object is iterated by key-value pairs through for--of loop return 2 member array of [key, value] for each iteration. Iteration happens in insertion order, which corresponds to the order in which each key-value pair was inserted into the map by the set() method.

Property of Map:

  1. The first property of the Map is its size.

    The size accessor property returns the number of elements in a Map object.

    
     const map1 = new Map();
    
     map1.set('a', 'alpha');
     map1.set('b', 'beta');
     map1.set('g', 'gamma');
     map1.set('shushil',{phone:"393975782", address:"123 N 1st Ave"})
    
     console.log(map1.size);// Expected output: 4
     console,log(map1.get('a'); // Output : alpha
    

    Map() constructor

    The Map() constructor creates Map objects.

    Syntax

    new Map()

    new Map(Iterable)

    Creating a new Map

     //New Map (iterable) or parameters
     const myMap=new Map([[1,'one'],[2,'two'],[3,'three']])
     // new Map
     const myMap=new Map();
    

    Instance Methods

    1. The clear() method is used to remove all elements from a Map object.

    Demo:

     // Clear()
     const map1 = new Map();
     map1.set('name', "Sushil")
     map1.set('surname', "chuabey");
    
     console.log(map1.size);  // Output: 2
    
     map1.clear();
    
     console.log(map1.size) // Output: 0
    

    2. The delete() method is used to remove the specified element from a Map object by key.

    Demo:

     const map1 = new Map();
     map1.set('name', "Sushil")
     map1.set('surname', "chuabey");
    
     console.log(map1.size); // Output:2
    
     console.log(map1.delete('name')); // delete "name" 
     console.log(map1); // Output : 'surname', 'chuabey'
    

    3. The has() method returns a boolean indicating whether an element with the specified key exits or not.

    Demo:

     const map1 = new Map();
     map1.set('name', "Sushil")
     map1.set('surname', "chuabey");
    
     console.log(map1.size);
     console.log(map1.has('name'); // output: True;
    

    4. The keys() method returns a new iterator object that contains the keys for each element in the Map object in insertion order.

    Demo:

     const map1 = new Map();
    
     map1.set('0', 'foo');
     map1.set(1, 'bar');
    
     const iterator1 = map1.keys();
     console.log(iterator1); //  Expected output: {0,1)}
    
     console.log(iterator1.next().value); 
     // Expected output: "0"
    
     console.log(iterator1.next().value);
     // Expected output: 1
    

    5. The values() the method returns a new iterator object that contains the values for each element in the Map object in insertion order.

    Demo:

     const map1=new Map()
     map1.set('0', 'shusil');
     map1.set(1, 'chaubey');
    
     const iterator1 = map1.values();
     console.log(iterator1); 
      // Output: {"shusil", "chaubey"}
    
     console.log(iterator1.next().value);
     // Expected output: "shusil"
    
     console.log(iterator1.next().value);
     // Expected output: "chaubey"
    

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

    Demo:

     const map1 = new Map();
    
     map1.set('0', 'shushil');
     map1.set(1, 'chaubey');
    
     const iterator1 = map1.entries();
     console.log(iterator1); 
     // Expected output : Array {["0", "shushil"],[1,"chaubey"]}
    
     console.log(iterator1.next().value);
     // Expected output: Array ["0", "shushil"]
    
     console.log(iterator1.next().value);
     // Expected output: Array [1, "chaubey"]