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 methods of the set collection in JavaScript

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

Share

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

What are the methods of set collection in JavaScript? For this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more small partners who want to solve this problem find a simpler and easier way.

Set methods are: 1. add(), used to add elements to the collection;2. delete(), used to delete an element in the collection;3. has(), used to determine whether the specified element exists in the collection;4. clear(), used to empty the collection elements;5. forEach(), used to traverse the elements in the collection.

Operating environment of this tutorial: Windows 7 system, Javascript version 1.8.5, Dell G3 computer.

Set Overview

A Set is similar to an Array, but a Set stores a key, which means that no two keys with the same value and data type can exist in a Set.

Set cannot be subscripted.

Set does not have length property but size

Set collections can be converted to real arrays via Array.from

Set attributes and methods (common) name Parameter type description size No attributes Get the length of the collection addObject method Add elements to the collection deletekey method Delete an element in the collection, delete successfully returns truehaskey method Determine whether the specified element exists in the collection, if it exists return trueclear Empty method Empty elements of the collection forEachfunction method Traverse elements in the collection

size property

var set = new Set(["sd",68,86,38,64,"qweq",58,"68",86]);console.log(set.size) //Print 8console.log(set.length) //Print undefined

add method

var set = new Set (["sd",68,86,38,64,"qweq",58,"68",86]); console.log (set.add("qq")); //Print {"sd",68,86,38,64,"qweq",58,"68",86,"qq"} to indicate that the console.log was added successfully (set.add("qq")); //still prints {"sd",68,86,38,64,"qweq",58,"68",86,"qq"} indicating that duplicates have not been added

delete method

var set = new Set(["sd",68,86,38,64,"qweq",58,"68",86]);console.log(set.delete("68")); //print true to indicate successful deletion console.log (set.delete("68")); //print false indicates deletion failed because "68"console.log(set) no longer exists in the collection; //Print {"sd",68,86,38,64,"qweq",58,86} "68 deleted"

has methods

var set = new Set(["sd",68,86,38,64,"qweq",58,"68",86]);console.log(set.has(68)); //Returns true indicating that 68 exists in the set.delete(68); //Here delete 68 console.log(set.has(68)); //returns false, 68 does not exist in the collection

clear method

var set = new Set(["sd",68,86,38,64,"qweq",58,"68",86]);set.clear(); //Empty the collection console.log (set.size); //Print 0 to indicate that the collection has been emptied console.log (set); //Print results {} Means the collection has been emptied.

forEach method

var set = new Set(["sd",68,86,38,64,"qweq",58,"68",86]);set.forEach(function(item,index,set){ console.log(item,index,set); //print result item is the value of each set element index is consistent with the result of item set is the set itself Javascript is a dynamically typed, weakly typed language, object-based and event-driven scripting language with relative security and widely used for client-side web development, and is also a scripting language widely used for client-side web development. It was mainly used to add dynamic functionality to HTML pages, and now JavaScript can also be used for web servers such as Node.js.

About JavaScript set collection methods have what questions to share here, I hope the above content can have some help for everyone, if you still have a lot of doubts not solved, you can pay attention to the industry information channel to learn more related knowledge.

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