Set is a data structure used to store ordered data. The elements in Set are unique and are not allowed to store the same elements; Set() can accept an iterable object as a parameter, but will Identical content in this iterable object is removed, so it can be used to remove duplicate elements and prevent "Array.from(new Set(arr))" or "[...new Set(arr)]".
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
Set
is a new data structure provided by ES6
, it is the same as an array It is used to store ordered data, but it does not have random access capabilities. That is to say, it cannot obtain a specific element through indexing like an array. In addition, the most important thing is that the elements in Set
are unique and the same elements are not allowed to be stored!
Set
is a constructor used to instantiate an instance:
let set = new Set() set.add(1)//往set集合中添加元素1
In addition, Set()
can accept a The iterable object is used as a parameter as the instance initialization data, but the same content in the iterable object will be removed. However, this is also a method of array deduplication.
let set = new Set([1,2,2,1,4,3,5]) console.log(set)//Set(5) {1, 2, 4, 3, 5}
The uniqueness of elements can be used to deduplicate arrays:
//方法一: Array.from(new Set(arr)) //arr是待去重的数组 //方法二: [...new Set(arr)]
How cool. Similarly, this feature can also be used to deduplicate the same characters in a string. .
[...new Set(str)].join('')
However, the above are all achieved through the uniqueness of Set
type elements, so how does Set
internally determine whether an element is unique? It uses an algorithm internally Same-value-zero equality
, which is roughly the same as the equality operator. The difference is that this algorithm considers NaN
equal to NaN
.
On Set.prototype
, An attribute size
is defined to represent the number of elements.
let set = new Set([1,2,2,1,4,3,5]) console.log(set.size)//5
#Set
Instance methods can be divided into two categories: operation methods and traversal methods.
Set.prototype.add(value)
—— Add a value to Set At the end of
, returns Set
itself. Set.prototype.delete(value)
—— Delete a certain value and return a Boolean value to indicate whether the deletion was successful. Set.prototype.has(value)
- Returns a Boolean value indicating whether the value is an element of Set
. Set.prototype.clear()
—— Clear all members, no return value. It is worth mentioning that the return of the add()
method is Set
itself, so you should be able to think of chain calls:
let set = new Set() set.add(1).add(2).add(3)
—— Returns the traverser of key names
—— Returns a traverser of key-value pairs
—— Returns a traverser of key-value pairs
—— Use the callback function to traverse the elements
Set structure has no key names, only key values ( In other words, the key name and key value are the same value), so the
keys method behaves exactly the same as the
values method.
is an upgraded version of Set
, there are two main ones Difference:
const ws = new WeakSet() ws.add(1)//报错,Invalid value used in weak set
Then the second point, the objects in
WeakSet are Weak reference. This means that the garbage collection mechanism will not consider WeakSet
's reference to the object. Once the external reference count reaches 0, it will wait to be processed by the garbage collection mechanism. Therefore, WeakSet
is suitable for temporarily storing a group of objects. Due to this feature, members in
are not suitable for reference because it is likely to be cleaned up at any time. However, ES6
stipulates that it is not traversable . The method in
is basically the same as the Set
mentioned above, but it does not have the size
attribute and no traverser method. 【Related recommendations:
The above is the detailed content of What is the use of es6 set?. For more information, please follow other related articles on the PHP Chinese website!