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 is a Set object in js

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

Share

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

This article will explain in detail what is the Set object in js. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

What is a Set object and how does it work?

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

We can use the Set constructor to create an Set instance.

Const set1 = new Set (); const set2 = new Set (["a", "b", "c", "d", "d", "e"])

We can use the add method to add a new value to the Set instance, because the add method returns a Set object, so we can use add again in a chained manner. If a value already exists in the Set object, it will no longer be added.

Set2.add ("f"); set2.add ("g"). Add ("h"). Add ("I"). Add ("j"). Add ("k"). Add ("k"); / / the latter "k" will not be added to the set object because it already exists

We can use the has method to check whether a specific value exists in the Set instance.

Set2.has ("a") / / trueset2.has ("z") / / true

We can use the size property to get the length of the Set instance.

Set2.size / / returns 10

You can use the clear method to delete data in Set.

Set2.clear ()

We can use the Set object to remove duplicate elements from the array.

Const numbers = [1, new Set 2, 3, 4, 5, 6, 7, 8, 5]; const uniqueNums = [. New Set (numbers)]; / / [1, 2, 4, 4, 5, 5, 7, 7, 5]

There is also WeakSet, which, like Set, is a collection of values that do not repeat. However, members of a WeakSet can only be objects, not values of other types. Objects in WeakSet are weak references, that is, the garbage collection mechanism does not take into account WeakSet's reference to the object.

Map data structure. It is similar to objects and is also a collection of key-value pairs, but the scope of "keys" is not limited to strings, and all types of values (including objects) can be used as keys.

The WeakMap structure, similar to the Map structure, is also a collection used to generate key-value pairs. However, WeakMap only accepts objects as key names (except null) and does not accept other types of values as key names. And the object pointed to by the key name of WeakMap is not included in the garbage collection mechanism.

This is the end of this article on "what is a Set object in js". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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