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 javascript removes elements from set

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how javascript removes elements from set. I hope you will get something after reading this article. Let's discuss it together.

Methods to delete elements: 1, use delete (), you can delete the specified element from the Set object, syntax "setObj.delete (value);"; 2, use clear (), you can delete all elements in the Set object, syntax "setObj.clear ();".

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Overview of Set Collection

The Set collection is very similar to the Arry array, but the Set collection stores key, which means that there cannot be two key with equal values and data types in the Set collection.

Set collection cannot be valued with subscript

The Set collection has no length attribute but size

The Set collection can be converted to a real array through Array.from

Javascript removes elements from set

1. Use delete ()

The delete () method removes the specified element from a Set object.

Syntax: setObj.delete (value)

Value: the element to be deleted

Return value:

True is returned for successful deletion, otherwise false is returned.

Example

Var mySet = new Set (); mySet.add ("foo"); mySet.delete ("bar"); / / returns false, does not contain the element "bar" mySet.delete ("foo"); / / returns true, and successfully deletes mySet.has ("foo"); / / returns false. "foo" has been deleted successfully.

2. Use clear ()

The clear () method removes all elements from the current Set object.

Syntax: setObj.clear ()

Example:

Var set = new Set (["sd", 68 set, 58, "68", 86]); set.clear (); / / empty the collection console.log (set.size); / / print a result of 0 indicating that the collection has been emptied console.log (set) / / print result {} indicates that the collection has been emptied. After reading this article, I believe you have some understanding of "how javascript removes elements from set". If you want to know more about it, please follow the industry information channel. Thank you for reading!

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