JavaScript Map Objects
A Map object holds key-value pairs where the keys can be any datatype.
A Map object remembers the original insertion order of the keys.
A Map object has a property that represents the size of the map.
Essensial Map() Methods
| Method | Description | 
|---|---|
| new Map() | Creates a new Map object | 
| set() | Sets a value for a key in a Map object | 
| get() | Gets a value for a key in a Map object | 
| entries() | Returns an array of the key/value pairs in a Map object | 
| keys() | Returns an array of the keys in a Map object | 
| values() | Returns an array of the values in a Map object | 
Map() Properties
| Property | Description | 
|---|---|
| size | Gets a value for a key in a Map object | 
Create a Map Object
Being able to use an Object as a key is an important Map feature.
Example
// Create Objects
const apples = {name: 'Apples'};
const bananas = {name: 'Bananas'};
const oranges = {name: 'Oranges'};
// Create a new Map
const fruits = new Map();
// Add new Elements to the Map
fruits.set(apples, 500);
fruits.set(bananas, 300);
fruits.set(oranges, 200);
Try it Yourself »
The get() method gets a value for a key in a Map object:
Example
fruits.get(apples);    // Returns 500
Try it Yourself »
fruits.get("apples");  // Returns undefined
Try it Yourself »
You can pass an Array to the new Map() constructor:
Example
// Create Objects
const apples = {name: 'Apples'};
const bananas = {name: 'Bananas'};
const oranges = {name: 'Oranges'};
// Create a new Map
const fruits = new Map([;
  [apples, 500],
  [bananas, 300],
  [oranges, 200]
]);
Try it Yourself »
Other Map() Methods
| Method | Description | 
|---|---|
| clear() | Removes all elements in a Map | 
| delete() | Removes an element specified by a key. | 
| has() | Returns true if a key exists. | 
| forEach() | Invokes a callback for each key/value pair. | 
