Introduction to Maps in javascript with a simple example

This article explains the concepts of maps in javascript in details with some real world example. Introduction to Maps in javascript with a simple example.

Maps data structure was introduced in Javascript ES6 to solve some problem that comes when we use javascript objects.

What is Maps

Maps are key/value pair data structure in javascript. maps are similar to javascript objects except the fact that it can contain any data type as key.

Maps key can be of any data type, it can be string,number or an object itself.

there are other different characteristics also between object and Maps.

Key Field - Key Field can be of any type. it can be string, number or an object or array too.

Field - Key Field can be of any type. it can be string, number or an object or array too. Element Orders - Map data structure maintains the order of elements.

- Map data structure maintains the order of elements. Instance of - Basically, Map is an instance of object whereas object is not an instance of Map.

1 let data = new Map ( ) 2 3 let val = { 4 id : 1 , 5 } 6 7 console . log ( data instanceof Object ) 8 9 console . log ( val instanceof Map )

So, the output will be like,

Creating Map in Javascript

map can be created using keyword new with Map.

1 let data = new Map ( )

this is an initialization of map which is stored in the variable data.

Adding items to Map

you can add items to Map using a method set() which basically sets the value to key.

1 data . set ( 1 , { 2 address : { 3 country : "India" , 4 postal : 123456 , 5 } , 6 } ) 7 data . set ( 2 , { 8 address : { 9 country : "US" , 10 postal : 123456 , 11 } , 12 } ) 13 14 console . log ( data )

So, it sets the value to data with key 1 and 2.

Getting item using key

you can get the value from map using the key in Map data structure. Map uses method get() which returns the value from the key.

1 console . log ( data . get ( 1 ) ) 2 3 console . log ( data . get ( 2 ) )

Deleting a item using key

you can delete an item from map using key. delete() is a method used to delete an item from map.

1 console . log ( data . delete ( 2 ) ) 2 console . log ( data )

Deleting all items

you can delete all items in map using the method clear().

1 2 data . clear ( )

Iterating over map keys

you can iterate over all the keys in the map using map.keys()

1 2 for ( const k of data . keys ( ) ) { 3 console . log ( k ) 4 }

Iterating over map values

Similarly, you can iterate over all the values in the map using map.values().

1 2 for ( const k of data . values ( ) ) { 3 console . log ( k ) 4 }

So, it will output like,

Iterating over map key,value pairs

1 2 for ( const [ k , v ] of data ) { 3 console . log ( k , v ) 4 }

Complete Code

WeakMaps in Javascript

WeakMap is also a Map except the fact that it has garbage collection. if a key lost the reference, then particular value from the WeakMap will be garbage collected.

Let's see an example to understand the concept,

1 let user = { 2 name : "Steve" , 3 } 4 5 let weakMap = new WeakMap ( ) 6 weakMap . set ( user , "check" ) 7 8 user = null 9 10 console . log ( weakMap )

can you guess what is the output of it?.

Here, we have user variable which is basically a key for weakmap. if you null the user variable, weakMap lost the reference for key.

it will be garbage collected automatically in WeakMap.

Summary

To sum up, These are all the concepts of Maps and WeakMaps in Javascript.

Recent Articles

How to Run MongoDB as a Docker Container in Development

To Read More