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

What are the set methods of es6

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what are the set methods of es6". In the daily operation, I believe that many people have doubts about the set methods of es6. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions of "what are the set methods of es6?" Next, please follow the editor to study!

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

ES6 provides a new data structure, Set. It is similar to an array, but the values of the members are unique and there are no duplicate values.

Many times we call Set a collection, but Set can be a collection, and a collection is not necessarily a Set.

Characteristics: uniqueness = > do not repeat = > can deduplicate the data.

Create Set

Set itself is a constructor that is called to generate Set data structures.

Keyword identifier = new Set ()

Example

Let I = new Set ()

The Set function can take an array (or an array-like object) as an argument for data initialization.

Let I = new Set ([1,2,3,4,4]); / / you will get set {1,2,3,4,}

Note: if the value given during initialization is duplicated, it will be removed automatically. Collections are not literally declared and can only be declared using the new keyword.

Properties of Set

There is only one commonly used property: size-- returns the total number of members of the Set instance.

Let s = new Set ([1,2,3]); console.log (s.size); / / the method of 3Set

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

Method of operation:

Add (value) adds data and returns a new Set structure

Delete (value) deletes the data and returns a Boolean value indicating whether the deletion is successful

Has (value) checks to see if some data exists and returns a Boolean value

Clear () clears all data and returns no value

Let set = new Set ([1, 2, 3, 4, 4]); / / add data 5let addSet = set.add (5); console.log (addSet); / / Set (5) {1, 2, 3, 4, 5} / / Delete data 4slet delSet = set.delete (4); console.log (delSet); / / true the return value here is a boolean indicating whether the deletion is successful / / check whether there is data 4let hasSet = set.has (4) Console.log (hasSet); / / false// clears all data set.clear (); console.log (set); / / Set (0) {}

Traversal method:

Set provides three traversal generator functions and a traversal method.

Keys () returns an ergodic with a key name

Values () returns an ergodic with a key value

Entries () returns an ergodic of a key-value pair

ForEach () uses the callback function to iterate through each member

Let color = new Set (["red", "green", "blue"]); for (let item of color.keys ()) {console.log (item);} / / red// green// bluefor (let item of color.values ()) {console.log (item);} / / red// green// bluefor (let item of color.entries ()) {console.log (item) } / / ["red", "red"] / / ["green", "green"] / / ["blue", "blue"] color.forEach ((item) = > {console.log (item)}) / / red// green// blue at this point, the study of "what are the set methods of es6" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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