JavaScript Map Data Structure

Ahmadullahnikzad
3 min readFeb 3, 2021

As I wrote about Set Data Structure in the last article today I intend to teach you a little bit about Map Data Structure

JavaScript Maps are objects designed to efficiently store and retrieve items based on a unique key for each item. In map key and values can be primitive, object, or both of them. In map, like objects, we can iterate through items and return key-value pair items.

Creating a Map

We create a map using Map Constructor.

Adding Items

Using the set() method we can add items to a map. this method this two arguments,the initial one is for key and the second one is for value.

Unlike Sets that discard duplicate items but map overwrite that duplicate key’s value.

Deleting Items

Using delete() method requires one argument which is the name of that item.

If you wanna delete all items of a Set use clear() method which doesn’t take any argument to be passed in.

Searching for Items

Sometimes you wanna search for a specific item in a Set use has() method which takes one argument that argument is the key of that object you’re seeking for. if that item is available returns true else returns false.

Returning the value of a Map item

Using the get() method we can return the value of a map.

It’s possible to get all the keys and values using keys() and values() method. these methods return MapIterator object which has next() method which can be used to loop through the items of the map.

As with Sets, we could use forEach and for…of either which use with Map.

Follow me on Facebook and Twitter

--

--

Ahmadullahnikzad

I am a web developer who writes web development content for you to make web development easier