A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Map in ES6

Map in ES6

Array Map

  • Map is a collection of key data items any type of keys are allowed in Map.

  • The Map object holds key-value pairs and handles the same insertion order of the keys.

  • A Map object traverse its elements in insertion order and the method does not change the original array.

  • It returns the new or empty Map with the results of calling a function for every array element.

  • The map() method does not execute the function for array elements without values.

  • A Map does not contain any keys by default. It only contains the values provided by user.

new Map() – creates the map.

map.set(key, value) – stores the value by the key.

map.get(key) – returns the value by the key, undefined if key doesn’t exist in map.

map.has(key) – returns true if the key exists, false otherwise.

map.delete(key) – removes the value by the key.

map.clear() – removes everything from the map.

map.size – returns the current element count.


Program for Map:-

let map = new Map([[1 , 2], [2 ,3 ] ,[4, 5]]); 

let map2 = new Map([["firstname" ,"Aman"],  ["lastname", "Singh"], ["Address", "Yamunanagar"]]); 

{1 => 2, 2 => 3, 4 => 5}

{"firstname" => "Aman", "lastname" => "Singh", "Address" => "Yamunanagar"}


Program for Map with set method:-

let map = new Map();  
    map.set('Aman', 'Singh');  
    map.set('Nittu', 'Singh');  
    map.set('Heera', 'Babu');  
    map.set('Singham', 'Singh');