Map is a collection of keyed data items, just like an 5. But the main difference is that 3 allows keys of any type. Show Methods and properties are:
For instance:
As we can see, unlike objects, keys are not converted to strings. Any type of key is possible.
Although 9 also works, e.g. we can set 2, this is treating 3 as a plain JavaScript object, so it implies all corresponding limitations (only string/symbol keys and so on).So we should use 3 methods: 5, 6 and so on.Map can also use objects as keys. For instance:
Using objects as keys is one of the most notable and important 3 features. The same does not count for 5. String as a key in 5 is fine, but we can’t use another 5 as a key in 5.Let’s try:
As 2 is an object, it converts all 5 keys, such as 4 and 5 above, to same string 6. Definitely not what we want.How To test keys for equivalence, 3 uses the algorithm . It is roughly the same as strict equality 9, but the difference is that 0 is considered equal to 0. So 0 can be used as the key as well.This algorithm can’t be changed or customized. Chaining Every 3 call returns the map itself, so we can “chain” the calls:
For looping over a 3, there are 3 methods:
For instance:
The insertion order is used The iteration goes in the same order as the values were inserted. 3 preserves this order, unlike a regular 5.Besides that, 3 has a built-in 3 method, similar to 4:
When a 3 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 3 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:
Here, 7 returns the array of key/value pairs: 8. That’s what 3 needs.We’ve just seen how to create 3 from a plain object with 1.There’s 2 method that does the reverse: given an array of 8 pairs, it creates an object from them:
We can use 2 to get a plain object from 3.E.g. we store the data in a 3, but we need to pass it to a 3rd-party code that expects a plain object.Here we go:
A call to 7 returns an iterable of key/value pairs, exactly in the right format for 2.We could also make line 9 shorter: 0That’s the same, because 2 expects an iterable object as the argument. Not necessarily an array. And the standard iteration for 3 returns same key/value pairs as 7. So we get a plain object with same key/values as the 3.A 4 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 7 with the same value don’t do anything. That’s the reason why each value appears in a 4 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. 4 is just the right thing for that: 1The alternative to 4 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. 4 is much better optimized internally for uniqueness checks.We can loop over a set either with 9 or using 3: 2Note the funny thing. The callback function passed in 3 has 3 arguments: a 0, then the same value 06, and then the target object. Indeed, the same value appears in the arguments twice.That’s for compatibility with 3 where the callback passed 3 has three arguments. Looks a bit strange, for sure. But this may help to replace 3 with 4 in certain cases with ease, and vice versa.The same methods 3 has for iterators are also supported:
3 – is a collection of keyed values.Methods and properties:
The differences from a regular 5:
4 – is a collection of unique values.Methods and properties:
Iteration over 3 and 4 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. |