JavaScript Map Methods
new Map() method
You can pass an array to the new Map()
Constructor to create Map:
method
// Create a Map const fruits = new Map([ ["apples", 500], ["bananas", 300], ["oranges", 200] ]);
Map.get()
You can use get()
The method gets the value of a key in the Map:
method
fruits.get("apples");
Map.set()
You can use set()
The method adds elements to the Map:
method
// Create a Map const fruits = new Map(); // Set Map values fruits.set("apples", 500); fruits.set("bananas", 300); fruits.set("oranges", 200);
set()
The method can also be used to change existing Map values:
method
fruits.set("apples", 500);
Map.size
size
The property returns the number of elements in the Map:
method
fruits.size;
Map.delete()
delete()
The method removes one element from the Map:
method
fruits.delete("apples");
Map.clear()
clear()
The method removes all elements from the Map:
method
fruits.clear();
Map.has()
If a key exists in the Map:has()
The method returns true:
method
fruits.has("apples");
Try the following code:
fruits.delete("apples"); fruits.has("apples");
Map.forEach()
forEach()
The method calls the callback function for each key-value pair in the Map:
method
// List all entries let text = ""; fruits.forEach(function(value, key) { text += key + ' = ' + value; });
Map.entries()
entries()
The method returns an array containing the [key, value] The iterator object:
method
// List all entries let text = ""; for (const x of fruits.entries()) { text += x; }
Map.keys()
keys()
The method returns an iterator object containing the keys of the Map:
method
// List all keys let text = ""; for (const x of fruits.keys()) { text += x; }
Map.values()}
values()
method returns an iterator object containing the values in the Map:
method
// List all values let text = ""; for (const x of fruits.values()) { text += x; }
You can use values()
method sums the values in the Map:
method
// Sum all values let total = 0; for (const x of fruits.values()) { total += x; }
Object as key
Hint:The ability to use objects as keys is an important feature of Map.
method
// 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);
Note:The key is the object (apples), not the string ("apples"):
method
fruits.get("apples"); // Returns undefined
JavaScript Map.groupBy()
ES2024 added to JavaScript Map.groupBy()
method.
Map.groupBy()
method groups the elements of the object based on the string value returned by the callback function.
Map.groupBy()
does not change the original object.
method
// Create an array const fruits = [ {name: "apples", quantity: 300}, {name: "bananas", quantity: 500}, {name: "oranges", quantity: 200}, {name: "kiwi", quantity: 150} ]; // Callback function for grouping function myCallback({ quantity }) { return quantity > 200 ? "ok" : "low"; } // Group by quantity const result = Map.groupBy(fruits, myCallback);
Browser Support
Map.groupBy()
It is an ES2024 feature.
Starting from March 2024, all new browsers support this feature:
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 117 | Edge 117 | Firefox 119 | Safari 17.4 | Opera 103 |
September 2023 | September 2023 | October 2023 | October 2024 | May 2023 |
Warning:
ES2024 features are relatively new.
Old browsers may require alternative code (Polyfill).
The difference between Object.groupBy() and Map.groupBy()
Object.groupBy()
and Map.groupBy()
The difference lies in:
Object.groupBy()
Group elements into a JavaScript object.
Map.groupBy()
Group elements into a Map object.
Complete Map Reference Manual
For a complete reference, please visit our:JavaScript Map Reference Manual.
This manual includes descriptions and examples of all Map properties and methods.