Content dated before 2011-04-08 (UTC) is licensed under CC BY-SA 2.5. Content dated from 2011-04-08 up to but not including 2018-05-02 (UTC) is licensed under CC BY-SA 3.0. Content dated on or after 2018-05-02 (UTC) is licensed under CC BY-SA 4.0. | Privacy Show
In JavaScript, An object is created with curly braces On the other hand, Until ES6 (ECMAScript
2015), JavaScript However, there were a few shortcomings,
With the introduction of ES6, we got two new data structures that address the shortcomings mentioned above:
In other words,
ADVERTISEMENT How to Create and Initialize a Map in JavaScriptA new
Which returns an empty
Another way of creating a
Which returns a
How to Add values to a Map in JavaScriptTo add value to a Map, use the The
Output:
Please note, if you use the same key to add a
value to a
So the output would be:
How to Get values from a Map in JavaScriptTo get a value from a
ADVERTISEMENT All About Map Keys in JavaScript
Here is the output:
A regular JavaScript object always treats the key as a string. Even when you pass it a primitive or object, it internally converts the key into a string:
Map Properties and Methods in JavaScriptJavaScript's
MapIterator: keys(), values(), and entries() in JavaScriptThe methods First, create a simple
ADVERTISEMENT How to Iterate Over a Map in JavaScriptYou can use either the
The output is going to be the same in both cases:
How to Convert an Object into a Map in JavaScriptYou may encounter a situation where you need to convert an
How to Convert a Map into an Object in JavaScriptIf you want to do the reverse, you can use the
ADVERTISEMENT How to Convert a Map into an Array in JavaScriptThere are a couple of ways to convert a map into an array:
Map vs. Object: When should you use them?
The similarity with objects ends here though. As you've seen, Use
Use an object when:
Set in JavaScriptA A ADVERTISEMENT How to Create and Initialize a Set in JavaScriptA new
And the output will be an empty
Here's how to create a
Output:
Set Properties and Methods in JavaScript
I love cucumbers! How about adding one more? Oh no, I can't –
The output is the same as before – nothing got added to the
Now our salad
How to Iterate Over a Set in JavaScript
Output:
We can use a Interestingly, JavaScript tries to make As
With
ADVERTISEMENT How to Enumerate over a Set in JavaScriptWe can enumerate over a Set using
The output of both is:
Sets and Arrays in JavaScriptAn array, like a The major difference between an array and a Think of How to Convert a Set into an array in JavaScriptConverting a
ADVERTISEMENT Unique values from an array using the Set in JavaScriptCreating a
Output:
Set and Object in JavaScriptA
Output: No surprise here – the Let's change a property of the object and add it to the set again:
What do you think the output will be? Two Here is the output:
Use In SummaryHere is a GitHub repository to find all the source code used in this article. If you found it helpful, please show your support by giving it a star: https://github.com/atapas/js-collections-map-set You may also like some of my other articles:
If this article was useful, please share it so others can read it as well. You can @ me on Twitter (@tapasadhikary) with comments, or feel free to follow me. Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started |