Object
, after all, a little more than a collection of key-value pairs. You can add, iterate, read and delete properties (properties) - all as in hash tables. Why then these new features in the language?Map
or Set
. However, there are problems with using normal objects:Object.create(null)
instead of simple {}
, or watch carefully that the built-in methods (such as Object.prototype.toString
) are not interpreted as data.for-of
loop, an operator ...
and so on.Map
and Set
- for other cases. To protect against collisions between data and embedded properties (properties), collections in ES6 do not expose data as properties. This means that you can not get to the data using expressions like obj.key
or obj[key]
. You have to write map.get(key)
. Also, entries in the hash table (as opposed to properties) are not inherited using the prototype chain.Map
and Set
, unlike ordinary objects, can have methods, both standard and custom, without conflicts.Set
is a set of values. It is changeable, so elements can be added and removed. Looks like a simple array, right? But there are differences.Set
, unlike an array, never contains one element twice. If you try to add an existing value, nothing will happen. > var desserts = new Set("abcd"); > desserts.size 4 > desserts.add("a"); Set [ "a", "b", "c", "d" ] > desserts.size 4
Set
can contain any objects. And, as with strings, when you try to add a duplicate, nothing will be added.Set
stores data in such a way that checking for the presence of an element in a set is very fast. > // , "zythum" . > arrayOfWords.indexOf("zythum") !== -1 // true > setOfWords.has("zythum") // true
Set
not available. > arrayOfWords[15000] "anapanapa" > setOfWords[15000] // set undefined
new Set
creates a new empty set.new Set(iterable)
creates a set and fills it with data from any iterable source .set.size
returns the number of elements in the set.set.has(value)
returns true if the set contains the given value.set.add(value)
adds an element to the set. As you remember, if you try to add an existing one, nothing will happen.set.delete(value)
removes an element from a set. Like add (), returns a reference to the set, which allows you to call methods one after another (a la Fluid Interface - approx .)set[Symbol.iterator]()
returns a new iterator over the values in the set. It is usually not called directly, but this is what makes the sets iterable. So you can write for (v of set) {...}
and so on.set.forEach(f)
easiest to explain in code. This is a brief record of the following expression: for (let value of set) f(value, value, set);
.forEach()
in arrays.set.clear()
removes all elements.set.keys(), set.values(), set.entries()
return various iterators. They are designed for compatibility with Map, so about them later.new Set(iterable)
because it works at the level of data structures. You can use it to convert an array into a Set
, remove duplicates into one line of code. Or pass the generator there, it will execute and assemble the elements into a multitude. This is also a method for copying an existing Set
.Set
is, there are methods that would be nice to include in the following versions of the standard:ap(), .filter(), .some()
and .every()
.set1.union(set2)
and set1.intersection(set2).
set.addAll(iterable), set.removeAll(iterable)
and set.hasAll(iterable)
Source: https://habr.com/ru/post/261187/
All Articles