JavaScript Map

Map is a set of key-value pairs, where keys are unique.

Creat a Map

Map() constructor can be used to create a Map in JavaScript.

var map1 = new Map();
ADVERTISEMENT

Insert Key-Value Pair

Map.set() method inserts a new key-value pair, or update the value if the key is already present.

map1.set(key, value);

Get Value for a Specific Key

Map.get() method returns the value of specified key from this Map.

var value = map1.get(key);

Delete a Specific Key

Map.delete() method deletes the specified key from this Map.

var isDeleted = map1.delete(key);

If Map has a Specific Key

Map.has() method checks if this Map has specified key.

var isKeyPresentInMap = map1.has(key);

Execute a Function for Each Key-Value Pair

Map.forEach() method executes a specified function for each key-value pair of this Map.

map1.forEach (function(value, key) {
    //code
})

JavaScript Map Tutorials

The following tutorials cover concepts on a JavaScript Map in detail with examples.

Conclusion

In this JavaScript Tutorial, we learned about Maps in JavaScript.