Get the App
SLTechnology News&Howtos  ›  Development  › 

How javascript removes elements from set

Shulou Source: shulou.com Published: 2022-06-02 02:45:23 10月03日 Update

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!

Tags: Elements objects syntax success methods arrays examples articles results similarity subscript two that is you can pass done attributes tutorials data more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux macOS Shulou Information MySQL Docker