In JavaScript, Set and Map are two kinds of objects which might be used for storing information in an ordered method. Each these information constructions are used to retailer distinct kinds of information inside the identical object. In Maps, the info is saved as a key-value pair whereas in Set information is a single assortment of values which might be distinctive.
Let’s be taught in regards to the makes use of of those two information constructions.
JavaScript Set: It’s a assortment of values that may be accessed with no particular key. The weather are distinctive and the addition of duplicates will not be allowed. Units are principally used to take away duplicates from some other information construction
Â
Â
Syntax:
new Set([it]);
Instance: On this instance, we are going to discover ways to implement a set
Javascript
|
Output:
Hey 1 Bye @
JavaScript Map: Maps are used to retailer information in key-value pairs the place keys are used to uniquely establish a component and values comprise the info related to it.
Syntax:
new Map([it])
Instance: On this instance, we are going to implement a map.
Javascript
|
Output:
["name","Ram"] ["Role","SDE"] ["Country","India"]
What to make use of?
Selecting one information construction amongst these two depends upon the utilization and the way we wish to entry the info. If we wish the info to be recognized by a key then we must always go for maps but when we wish distinctive values to be saved then we must always use a set.
Map | Set |
---|---|
It’s a assortment of key-value | It’s a assortment of distinctive parts |
Map is two-dimensional | The set is one dimensional |
Values are accessed utilizing keys | In-built strategies are used to entry  values |