JavaScript Map Data Structure

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

Adding Items

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

Deleting Items

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

Returning the value of a Map item

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

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