How to get to map
How to get to map
Map and Set
Till now, we’ve learned about the following complex data structures:
But that’s not enough for real life. That’s why Map and Set also exist.
Methods and properties are:
As we can see, unlike objects, keys are not converted to strings. Any type of key is possible.
Map can also use objects as keys.
This algorithm can’t be changed or customized.
Every map.set call returns the map itself, so we can “chain” the calls:
Iteration over Map
Besides that, Map has a built-in forEach method, similar to Array :
Object.entries: Map from Object
When a Map is created, we can pass an array (or another iterable) with key/value pairs for initialization, like this:
If we have a plain object, and we’d like to create a Map from it, then we can use built-in method Object.entries(obj) that returns an array of key/value pairs for an object exactly in that format.
So we can create a map from an object like this:
Object.fromEntries: Object from Map
There’s Object.fromEntries method that does the reverse: given an array of How to get to map pairs, it creates an object from them:
We could also make line (*) shorter:
A Set is a special type collection – “set of values” (without keys), where each value may occur only once.
Its main methods are:
The main feature is that repeated calls of set.add(value) with the same value don’t do anything. That’s the reason why each value appears in a Set only once.
For example, we have visitors coming, and we’d like to remember everyone. But repeated visits should not lead to duplicates. A visitor must be “counted” only once.
Set is just the right thing for that:
The alternative to Set could be an array of users, and the code to check for duplicates on every insertion using arr.find. But the performance would be much worse, because this method walks through the whole array checking every element. Set is much better optimized internally for uniqueness checks.
Iteration over Set
We can loop over a set either with for..of or using forEach :
That’s for compatibility with Map where the callback passed forEach has three arguments. Looks a bit strange, for sure. But may help to replace Map with Set in certain cases with ease, and vice versa.
The same methods Map has for iterators are also supported:
Summary
Map – is a collection of keyed values.
Methods and properties:
The differences from a regular Object :
Set – is a collection of unique values.
Methods and properties:
Iteration over Map and Set is always in the insertion order, so we can’t say that these collections are unordered, but we can’t reorder elements or directly get an element by its number.
How to get to map
The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value.
Try it
Description
Map objects are collections of key-value pairs. A key in the Map may only occur once; it is unique in the Map ‘s collection. A Map object is iterated by key-value pairs — a for. of loop returns a 2-member array of How to get to map for each iteration. Iteration happens in insertion order, which corresponds to the order in which each key-value pair was first inserted into the map by the set() method (that is, there wasn’t a key with the same value already in the map when set() was called).
The specification requires maps to be implemented «that, on average, provide access times that are sublinear on the number of elements in the collection». Therefore, it could be represented internally as a hash table (with O(1) lookup), a search tree (with O(log(N)) lookup), or any other data structure, as long as the complexity is better than O(N).
Key equality
Objects vs. Maps
Object is similar to Map —both let you set keys to values, retrieve those values, delete keys, and detect whether something is stored at a key. For this reason (and because there were no built-in alternatives), Object has been used as Map historically.
However, there are important differences that make Map preferable in some cases:
Map | Object | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Accidental Keys | A Map does not contain any keys by default. It only contains what is explicitly put into it. | The number of items in a Map is easily retrieved from its size property. | The number of items in an Object must be determined manually. | ||||||||||||||||||||||||||||||||||||
Iteration | A Map is an iterable, so it can be directly iterated. |
Method | Description |
---|---|
new Map() | Creates a new Map object |
set() | Sets the value for a key in a Map |
get() | Gets the value for a key in a Map |
clear() | Removes all the elements from a Map |
delete() | Removes a Map element specified by a key |
has() | Returns true if a key exists in a Map |
forEach() | Invokes a callback for each key/value pair in a Map |
entries() | Returns an iterator object with the How to get to map pairs in a Map |
keys() | Returns an iterator object with the keys in a Map |
values() | Returns an iterator object of the values in a Map |
Property | Description |
---|---|
size | Returns the number of Map elements |
How to Create a Map
You can create a JavaScript Map by:
new Map()
You can create a Map by passing an Array to the new Map() constructor:
Example
Map.set()
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
Map.get()
The get() method gets the value of a key in a Map:
Example
Map.size
The size property returns the number of elements in a Map:
Example
Map.delete()
The delete() method removes a Map element:
Example
Map.clear()
The clear() method removes all the elements from a Map:
Example
Map.has()
The has() method returns true if a key exists in a Map:
Example
Try This:
Maps are Objects
typeof returns object:
Example
instanceof Map returns true:
Example
JavaScript Objects vs Maps
Differences between JavaScript Objects and Maps:
Object | Map |
---|---|
Not directly iterable | Directly iterable |
Do not have a size property | Have a size property |
Keys must be Strings (or Symbols) | Keys can be any datatype |
Keys are not well ordered | Keys are ordered by insertion |
Have default keys | Do not have default keys |
Map.forEach()
The forEach() method invokes a callback for each key/value pair in a Map:
Example
Map.entries()
The entries() method returns an iterator object with the How to get to map in a Map:
Example
Map.keys()
The keys() method returns an iterator object with the keys in a Map:
Example
Map.values()
The values() method returns an iterator object with the values in a Map:
Example
You can use the values() method to sum the values in a Map:
Example
Objects as Keys
Being able to use objects as keys is an important Map feature.
Example
// 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);
Remember: The key is an object (apples), not a string («apples»):
Example
Browser Support
JavaScript Maps are supported in all browsers, except Internet Explorer:
Источники информации:
- How to get to javascript
- How to get to nice