Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the set of es6

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to use the set of es6". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Set, which means "collection", is a new data structure provided by ES6, similar to an array, but the values of members are unique and there are no duplicate values. The Set object allows the user to store a unique value of any type, whether the original value or an object reference.

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

What is Set?

Set (collection) is a new data structure provided by ES6, similar to an array, but the values of members are unique and there are no duplicate values.

Set objects allow you to store unique values of any type, whether they are original values or object references.

Set itself is a constructor used to generate Set data structures.

Special values in Set:

The values stored in the Set object are always unique, so you need to determine whether the two values are identical. There are several special values that require special treatment:

+ 0 and-0 are identical in storage judgment uniqueness, so they are not repeated.

Undefined and undefined are identical, so they do not repeat.

NaN and NaN are not identical, but only one can be stored in Set without repetition.

Set use ca

1. Used for array deduplication

Let arr = [3,5,2,2,5,5]; let setArr = new Set (arr) / / returns the set data structure Set (3) {3,5,2} / / method-es6. Deconstructing let unique1 = [... setArr]; / / after de-repeating the array [3LJ 5jue 2] / / method II Array.from () parses the class array into the array let unique2 = Array.from (setArr) / / after de-repeating the array [3LJ 5J2]

2. Used for string deduplication

Let str = "352255"; let unique = [. New Set (str)] .join ("); / / 352

3. Realize intersection, union and difference

Let a = new Set ([1,2,3]); let b = new Set ([4,3,2]); / / Union let union = new Set ([... a,... b]); / / Set {1,2,3,4} / / intersection let intersect = new Set ([... a] .filter (x = > b.has (x) / / set {2,3} / (a relative to b) difference let difference = new Set ([... a] .filter (x = >! b.has (x); / / Set {1} "how to use set of es6" ends here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report