Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement Set with JavaScript

Shulou Source: shulou.com Published: 2022-06-01 02:38:20 09月22日 Update

Editor to share with you how JavaScript to achieve Set, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Simple implementation of Set

Window.Set = window.Set | | (function () {function Set (arr) {this.items = arr? Unique (arr): []; this.size = size of this.items.length; / / Array} Set.prototype = {add: function (value) {/ / add an element. If the element already exists, skip it and return the Set structure itself. If (! this.has (value)) {this.items.push (value); this.size++;} return this;}, clear: function () {/ / clears all members with no return value. This.items = [] this.size = 0}, delete: function (value) {/ / delete a value and return a Boolean value indicating whether the deletion is successful. Return this.items.some ((v, I) = > {if (v = value) {this.items.splice (iMagne1) return true} return false})}, has: function (value) {/ / returns a Boolean value Indicates whether the value is a member of Set. Return this.items.some (v = > v = value)}, values: function () {return this.items},} return Set;} (); what JavaScript can do 1. It can make web pages interactive, such as responding to user clicks and providing users with a better experience. two。 You can process forms, verify user input, and provide timely feedback to save users time. 3. The page can be created dynamically according to the user's operation. 4 using JavaScript, you can set up some temporary information that cookie stores on the browser.

The above is all the content of the article "how to achieve Set in JavaScript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Users articles elements content Boolean members successful not so interactive information dynamic can be passed size most time more browser knowledge structure Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Linux Shulou Tech Info OPPO Reno Xiaomi