The Map object in JavaScript serves the purpose of associating keys with corresponding values. It retains each entry in the form of key-value pairs. This object facilitates operations on the elements, including searching, updating, and deleting, by utilizing the designated key.
Syntax
Example
new Map([iterable])
Parameter
iterable - It denotes an array along with other iterable entities whose constituents are structured as key-value pairs.
Points to remember
- A map object cannot contain the duplicate keys.
- A map object can contain the duplicate values.
- The key and value can be of any type (allows both object and primitive values).
- A map object iterates its elements in insertion order.
JavaScript Map Methods
Here is a compilation of JavaScript map methods along with their corresponding descriptions.
| Methods | Description |
|---|---|
| clear() | It removes all the elements from a Map object. |
| delete() | It deletes the specified element from a Map object. |
| entries() | It returns an object of Map iterator that contains the key-value pair for each element. |
| forEach() | It executes the specified function once for each key/value pair. |
| get() | It returns the value of specified key. |
| has() | It indicates whether the map object contains the specified key element. |
| keys() | It returns an object of Map iterator that contains the keys for each element. |
| set() | It adds or updates the key-value pairs to Map object. |
| values() | It returns an object of Map iterator that contains the values for each element. |