This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Smartphone icons created by Freepik - Flaticon
10.10 JS Object Maps
- A Map holds key-value pairs where the keys can be any datatype.
- A Map remembers the original insertion order of the keys.
- A Map has a property that represents the size of the map.
Map Methods
Method |
Description |
new Map() |
Creates a new Map object |
set() |
Sets the value for a key in a Map |
get() |
Gets the value for a key in a Map |
clear() |
Removes all the elements from a Map |
delete() |
Removes a Map element specified by a key |
has() |
Returns true if a key exists in a Map |
forEach() |
Invokes a callback for each key/value pair in a Map |
entries() |
Returns an iterator object with the [key, value] pairs in a Map |
keys() |
Returns an iterator object with the keys in a Map |
values() |
Returns an iterator object of the values in a Map |
Property |
Description |
size |
Returns the number of Map elements |
How to Create a Map
You can create a JavaScript Map by:
- Passing an Array to new Map()
- Create a Map and use Map.set()
new Map()
You can create a Map by passing an Array to the new Map() constructor:
// Create a Map
const fruits = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200]
]);
return to top of page
Map.set()
You can add elements to a Map with the set() method:
// Create a Map
const fruits = new Map();
// Set Map Values
fruits.set("apples", 500);
fruits.set("bananas", 300);
fruits.set("oranges", 200);
The set() method can also be used to change existing Map values:
fruits.set("apples", 500);
return to top of page
Map.get()
The get() method gets the value of a key in a Map:
fruits.get("apples"); // Returns 500
return to top of page
Map.clear()
The clear() method removes all the elements from a Map:
fruits.clear();
return to top of page
Map.has()
The has() method returns true if a key exists in a Map:
fruits.has("apples");
Try This:
fruits.delete("apples");
fruits.has("apples");
Try it yourself
return to top of page
Maps are Objects
typeof returns object:
// Returns object:
typeof fruits;
instanceof Map returns true:
// Returns true:
fruits instanceof Map;
JavaScript Objects vs Maps
Differences between JavaScript Objects and Maps:
Object |
Map |
Not directly iterable |
Directly iterable |
Do not have a size property |
Have a size property |
Keys must be Strings (or Symbols) |
Keys can be any datatype |
Keys are not well ordered |
Keys are ordered by insertion |
Have default keys |
Do not have default keys |
Map.forEach()
The forEach() method invokes a callback for each key/value pair in a Map:
// List all entries
let text = "";
fruits.forEach (function(value, key) {
text += key + ' = ' + value;
})
return to top of page
Map.entries()
The entries() method returns an iterator object with the [key,values] in a Map:
// List all entries
let text = "";
for (const x of fruits.entries()) {
text += x;
}
return to top of page
Map.keys()
The keys() method returns an iterator object with the keys in a Map:
// List all keys
let text = "";
for (const x of fruits.keys()) {
text += x;
}
return to top of page
Map.values()
The values() method returns an iterator object with the values in a Map:
// List all values
let text = "";
for (const x of fruits.values()) {
text += x;
}
You can use the values() method to sum the values in a Map:
// Sum all values
let total = 0;
for (const x of fruits.values()) {
total += x;
}
return to top of page
Objects as Keys
Being able to use objects as keys is an important Map feature.
// Create Objects
const apples = {name: 'Apples'};
const bananas = {name: 'Bananas'};
const oranges = {name: 'Oranges'};
// Create a Map
const fruits = new Map();
// Add new Elements to the Map
fruits.set(apples, 500);
fruits.set(bananas, 300);
fruits.set(oranges, 200);
Remember: The key is an object (apples), not a string ("apples"):
fruits.get("apples"); // Returns undefined
JavaScript icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon
Example files created in this module:
JS Variables - Objects are Variables part 1
JS Variables - Objects are Variables part 2
JS Objects - Using an Object Literal part 1
JS Objects - Using an Object Literal part 2
JS Objects - Using an Object Literal part 3
JS Objects - Using the Keyword new
JS Objects are Mutable
JS Object Properties - Access a property with .property
JS Object Properties - Access a property with ["property"]
JS Object Properties - Looping object property values
JS Object Properties - Add a new property to an existing object
JS Object Properties - Deleting object properties part 1
JS Object Properties - Deleting object properties part 2
JS Objects - Access nested objects part 1
JS Objects - Access nested objects part2
JS Objects - Access nested objects part 3
JS Objects - Access nested objects part 4
JS Arrays - Nested JS Objects and Arrays
JS Object Methods - this Keyword
JS Objects - Creating and using an object method part 1
JS Objects - Creating and using an object method part 2
JS Objects - Adding a Method to an Object
JS Objects - Using Built-In Methods
JS Objects - Displaying a JavaScript object
JS Objects - Display object properties
JS Objects - Displaying the Object in a Loop
JS Objects - Using Object.values()
JS Objects - Using JSON.stringify()
JS Objects - Stringify Dates
JS Objects - Stringify Functions part 1
JS Objects - Stringify Functions part 2
JS Arrays - Stringify Arrays
JS Getters and Setters - The get Keyword
JS Getters and Setters - The set Keyword
JS Object Methods part 1
JS Object Methods part 2
JS Getters and Setters - Data Quality part 1
JS Getters and Setters - Data Quality part 2
JS Getters and Setters - Object.defineProperty()
JS Object Constructors
JS Object Constructors - Add a Property to an Object
JS Object Constructors - Adding a Method to an Object
JS Object Constructors - Adding a Property to a Constructor part 1
JS Object Constructors - Adding a Property to a Constructor part 2
JS Object Constructors - Adding a Property to a Constructor part 3
JS Object Constructors - Adding a Method to a Constructor
Did You Know?
JS Objects - Using an object constructor
JS Objects - Using the prototype Property part 1
JS Objects - Using the prototype Property part 2
JS Iterables - Iterate over a String
JS Iterables - Iterating Over an Array
JS Iterables - Home Made Iterable part 1
JS Iterables - Home Made Iterable part 2
JS Sets - The new Set() Method part 1
JS Sets - The new Set() Method part 2
JS Sets - The new Set() Method part 3
JS Sets - The add() Method part 1
JS Sets - The add() Method part 2
JS Sets - The forEach() Method
JS Sets - The values() Method part 1
JS Sets - The values() Method part 2
JS Sets - The keys() Method
JS Sets - The entries() Method
JS Map Objects - new Map()
JS Map Objects - Map.set() part 1
JS Map Objects - Map.set() part 2
JS Map Objects - Map.get()
JS Map Objects - Map.size
JS Map Objects - Map.delete()
JS Map Objects - Map.clear()
JS Maps using Map.has()
JS Map Objects - Map typeof
JS Map Objects - Map instanceof
JS Map Objects - Using Map.forEach()
JS Map Objects - Using Map.entries()
JS Map Objects - Using Map.keys()
JS Map Objects - Using Map.values() part 1
JS Map Objects - Using Map.values() part 2
JS Map Objects - Objects as Keys part 1
JS Map Objects - Objects as Keys part 2
JS Object - Changing a Property Value
JS Objects - Listing All Properties
JS Objects - Listing Enumerable Properties
JS Objects - Adding a Property
JS Objects - Adding Getters and Setters
JS Objects - A Counter Example