JavaScript Maps
A Map holds key-value pairs where the keys can be any datatype.
A Map remembers the original insertion order of the keys.
Essential Map Methods
Method | Description |
---|---|
new Map() | Creates a new Map |
set() | Sets the value for a key in a Map |
get() | Gets the value for a key in a Map |
delete() | Removes a Map element specified by the key |
has() | Returns true if a key exists in a Map |
forEach() | Calls a function for each key/value pair in a Map |
entries() | Returns an iterator with the [key, value] pairs in a Map |
Property | Description |
size | Returns the number of elements in a Map |
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()
The new Map() Method
You can create a Map by passing an Array to the new Map()
constructor:
Example
// Create a Map
const fruits = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200]
]);
The set() Method
You can add elements to a Map with the set()
method:
Example
// 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:
Example
fruits.set("apples", 200);
The get() Method
The get()
method gets the value of a key in a Map:
Example
fruits.get("apples"); // Returns 500
The size Property
The size
property returns the number of elements in a Map:
Example
fruits.size;
The delete() Method
The delete()
method removes a Map element:
Example
fruits.delete("apples");
The has() Method
The has()
method returns true if a key exists in a Map:
Example
fruits.has("apples");
Try This:
fruits.delete("apples");
fruits.has("apples");
JavaScript Objects vs Maps
Differences between JavaScript Objects and Maps:
Object | Map | |
---|---|---|
Iterable | Not directly iterable | Directly iterable |
Size | Do not have a size property | Have a size property |
Key Types | Keys must be Strings (or Symbols) | Keys can be any datatype |
Key Order | Keys are not well ordered | Keys are ordered by insertion |
Defaults | Have default keys | Do not have default keys |
The forEach() Method
The forEach()
method calls a function for each key/value pair in a Map:
Example
// List all entries
let text = "";
fruits.forEach (function(value, key) {
text += key + ' = ' + value;
})
The entries() Method
The entries()
method returns an iterator object with the [key, values] in a Map:
Example
// List all entries
let text = "";
for (const x of fruits.entries()) {
text += x;
}
order levaquin 250mg generic order levofloxacin 500mg pills
dutasteride order order tamsulosin generic order ondansetron 4mg without prescription
aldactone over the counter buy spironolactone order diflucan 100mg online
acillin cost generic erythromycin 500mg erythromycin 250mg oral
order fildena without prescription order methocarbamol 500mg without prescription order methocarbamol without prescription
buy lamictal 50mg buy minipress 2mg generic order retin cream generic
order tadalis 20mg generic voltaren over the counter diclofenac 100mg tablet
indocin 75mg pills trimox 250mg oral trimox generic
purchase arimidex ecs netpharmaworld viagra order viagra 50mg pill
generique tadalafil 5mg tadalafil 40mg pas cher sildenafil 100mg generique pas cher
prednisone drug cheap sildenafil generic sildenafil uk
tadalafil 5mg kaufen ohne rezept cialis 10mg kaufen viagra generika
accutane 20mg generic accutane 20mg generic cost for ivermectin 3mg
provigil canada provigil order buy acetazolamide sale
buy altace 5mg online cheap astelin 10ml generic order azelastine
clonidine 0.1mg cost order minocycline 50mg online cheap order spiriva 9mcg without prescription
purchase buspirone online cheap buspar 10mg cheap order ditropan 5mg for sale
purchase hytrin online cheap actos 30mg us sulfasalazine 500mg pills
alendronate 70mg pills order ibuprofen for sale pepcid drug
olmesartan 20mg ca order acetazolamide 250mg sale buy generic diamox 250mg
tacrolimus cost buy prograf 5mg for sale urso tablet
buy imdur online cheap buy imdur sale order micardis 80mg for sale
order zyban zyrtec 10mg over the counter quetiapine sale
sertraline 100mg canada lexapro 10mg brand order sildenafil 100mg for sale
salbutamol brand order salbutamol online order sildenafil 50mg sale
order cialis 20mg Sale cialis viagra for sale online
cost tadalafil 5mg phenazopyridine online symmetrel 100 mg sale
avlosulfon sale buy perindopril 8mg generic aceon 4mg ca
provera 5mg generic cyproheptadine 4 mg pill purchase cyproheptadine online cheap
modafinil 100mg brand provigil 100mg canada ivermectin 12 mg otc
isotretinoin uk buy deltasone 10mg generic deltasone 10mg uk
order generic zithromax azithromycin 250mg brand order gabapentin 800mg without prescription
order furosemide 40mg online cheap hydroxychloroquine 200mg pills hydroxychloroquine ca
cheap tadalafil tablets cialis 20 mg price clomipramine uk
Hello bro!Show more>>>
http://bit.ly/3QIq0PD
порносериалы
http://mandyporter.com/__media__/js/netsoltrademark.php?d=videoterebonka.com http://7xgncew0c.net/__media__/js/netsoltrademark.php?d=videoterebonka.com http://danceolympics.net/__media__/js/netsoltrademark.php?d=videoterebonka.com http://1onions.com/__media__/js/netsoltrademark.php?d=videoterebonka.com http://louishagopian.com/__media__/js/netsoltrademark.php?d=videoterebonka.com
Hello bro!More info!..
https://mostbet-8by.xyz/
Davidham
http://images.google.kz/url?q=https://mostbet-8by.xyz https://images.google.at/url?q=https://mostbet-8by.xyz http://cse.google.com.uy/url?q=https://mostbet-8by.xyz https://cse.google.vg/url?q=https://mostbet-8by.xyz http://cse.google.rw/url?q=https://mostbet-8by.xyz
Hello bro!More info!..
порно игры торрент
секс порно изнасилование
http://viewpsot.com/__media__/js/netsoltrademark.php?d=pansionat-rnd.ru http://truthinaging.net/__media__/js/netsoltrademark.php?d=pansionat-rnd.ru http://ojairesortcd.com/__media__/js/netsoltrademark.php?d=pansionat-rnd.ru http://seniorsreadytorelocate.com/__media__/js/netsoltrademark.php?d=pansionat-rnd.ru http://thepeyton.company/__media__/js/netsoltrademark.php?d=pansionat-rnd.ru