Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use set in es6

Shulou Source: shulou.com Published: 2022-06-03 15:16:59 09月22日 Update

This article mainly explains "how to use set in es6". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use set in es6.

This article operating environment: windows7 system, ECMAScript version 6, Dell G3 computer.

How to use es6's set?

The usage of set in es6

As follows:

/ / the definition changes from var to let non-repeatable definition let a = [1 Set 2, 3, 4, 5]; / / it is similar to an array, but the values of the members are unique, and there are no duplicate values. Let set=new Set ()

The methods of Set instances fall into two main categories: operation methods (for manipulating data) and traversal methods (for traversing members).

Here are four ways to do it.

Add (value): adds a value that returns the Set structure itself.

Delete (value): deletes a value and returns a Boolean value indicating whether the deletion was successful.

Has (value): returns a Boolean value indicating whether the value is a member of Set.

Clear (): clears all members with no return value.

The Array.from method converts the Set structure into an array.

An instance of the Set structure has four traversal methods that can be used to traverse members.

Keys (): the traversal that returns the key name

Values (): the traversal that returns the key value

Entries (): a traversal that returns key-value pairs

ForEach (): use the callback function to iterate through each member

In particular, the traversal order of the Set is the insertion order.

Because the Set structure has no key name, only the key value (or the key name and the key value are the same value), the behavior of the keys method is exactly the same as that of the values method.

An instance of the Set structure is traverable by default, and its default traversal generation function is its values method.

This means that you can omit the values method and iterate through Set directly with a for...of loop.

The extension operator (...) uses a for...of loop internally, so it can also be used for Set structures.

Give an example

/ / 1 Array deduplicated let a = [1Partition 2, 3, 4, 5, 5, 6, 7, 1, 1, 7, 1, 1, 5]; let b=new Set ([... a]); b = [... b]; / / 2 to find the intersection and union difference set let a=new Set [1; 2, 5]; let b=new Set, [3, 4, 5]; / / intersection let c=new Set ([... a] .filter (x = > b.hasa [x])) / / intersection let d=new Set ([... a] .filter (x = >! Thank you for reading, this is the content of "how to use set in es6". After the study of this article, I believe you have a deeper understanding of how to use set in es6, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Methods members structures intersections examples that is arrays learning content functions Boolean order loop consistency success only large classes subtractions ideas situations Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Docker Apple MySQL macOS