Home > Web Front-end > Front-end Q&A > What is the use of es6 set?

What is the use of es6 set?

青灯夜游
Release: 2022-10-24 17:55:55
Original
1932 people have browsed it

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)]".

What is the use of es6 set?

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

1. Basic usage

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
Copy after login

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}
Copy after login

The uniqueness of elements can be used to deduplicate arrays:

//方法一:
Array.from(new Set(arr)) //arr是待去重的数组

//方法二:
[...new Set(arr)]
Copy after login

How cool. Similarly, this feature can also be used to deduplicate the same characters in a string. .

[...new Set(str)].join('')
Copy after login

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.

2. Instance properties and methods

Instance properties

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
Copy after login

Instance methods

#Set Instance methods can be divided into two categories: operation methods and traversal methods.

1. Operation method

  • 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)
Copy after login

2. Traversal method

  • ##Set.prototype.keys() —— Returns the traverser of key names
  • Set.prototype.values() —— Returns a traverser of key-value pairs
  • Set.prototype.entries() —— Returns a traverser of key-value pairs
  • Set.prototype.forEach() —— Use the callback function to traverse the elements
Since the

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.

3. WeakSet

##WeakSet

is an upgraded version of Set, there are two main ones Difference:

    WeakSet
  • can only store reference types, not basic type data. The reference types in
  • WeakSet
  • are all weak references.
  • First of all, the first point is easy to understand, that is, basic types of data cannot be stored:
const ws = new WeakSet()
ws.add(1)//报错,Invalid value used in weak set
Copy after login

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

WeakSet

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

WeakSet

is basically the same as the Set mentioned above, but it does not have the size attribute and no traverser method. 【Related recommendations:

javascript video tutorial

, programming video

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template