Loading Please Wait...
A Map holds key-value pairs where the keys can be any datatype. A Map remembers the original insertion order of the keys.
The JS Map has the following methods and properties:
The JS new Map() method is used to create a JS Map. You can pass variable, values, array, objects etc.
const map1 = new Map();
const map2 = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200]
]);
The JS set() method is used to add values to the Map. The set() method can also be used to update existing values to the Map.
const fruits = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200]
]);
fruits.set("mangos", 100); // set new values ["mangos", 100]
fruits.set("apples", 400); // update apples
The JS get() method is used to get values of a key in a Map.
const fruits = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200]
]);
fruits.get("bananas"); // 300
The JS forEach() method invokes (calls) a function for each key/value pair in a Map.
const fruits = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200]
]);
// List all Elements
let text = "";
fruits.forEach (function(value, key) {
text += key + ' = ' + value + "<br>"
});
The JS entries() method returns an iterator object with the [key, values] in a Map so you can use the Iterator object to access the elements.
const fruits = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200]
]);
fruits.entries() // JS Map Iterator having Map key,value pair
let text = "";
for (const x of fruits.entries()) {
text += x;
}
The JS has() method returns true if the Map has the passed key element.
const fruits = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200]
]);
fruits.has("apples"); // true
fruits.has("mangoes"); // false
The JS delete() method deletes the passed key element from the Map.
const fruits = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200]
]);
fruits.delete("apples"); // [["bananas", 300],["oranges", 200]]
The JS size property return the size of the Map.
const fruits = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200]
]);
fruits.sizs; // 3
How you feel about this blog:
Share this blog on:
If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com
Contact UsWe are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..
Copyright ©
, Lynxsia IT Solutions, All rights reserved