当前位置:网站首页>Set, weakset, map, weakmap in ES6
Set, weakset, map, weakmap in ES6
2022-07-03 20:30:00 【Liu Yiming】
One 、Set object
1. Definition
set:Set Object allows you to store any type of only value , Whether it's the original value or the object reference . Set Object is a set of values , You can iterate over its elements in the order of insertion . Set The elements in will only appear once , namely Set The elements in are unique ( The definition comes from mdn)set The most important thing in an object is the unique value
2. Usage method
(1)add add value
let set=new Set();
set.add(1);(2)delete Delete specific values
let set=new Set();
set.add(1);
set.delete(1);(3)clear Clear all internal values
let set=new Set();
set.add(1);
set.add(5);
set.clear()(4)has Check whether it contains a value
If there is a value , The return value is true
If it doesn't contain , The return value is false
let set=new Set();
set.add(1);
set.has(1);// The return value is true
set.has(0);// The return value is false(5)size
Inside size In fact, it is equivalent to our length
let set = new Set('abcdefg')
console.log(set.size);3. Skillfully use Set
It can be used set Achieve de duplication ,set The principle of is that the internal value is unique
let list=[1,2,3,41,2,1,1]
let arr=[...new Set(list)]
console.log(arr);Two 、WeakSet object
WeakSet: stay WeakSet Is unique in the set of It and Set There are two differences between objects : And Set comparison ,WeakSet It can only be a collection of objects , It can't be any value of any type . WeakSet Hold a weak quote : A reference to an object in a collection is a weak reference . If there's no other right WeakSet References to objects in , Then these objects will be regarded as The garbage Recycle . It also means that WeakSet There is no list of current objects stored in . Because of this ,WeakSet It can't be enumerated .( Quote from mdn) In my opinion, the meaning of non enumeration is non iterative cycle ,
In the use of WeakSet When , Be careful not to transfer , The following example is error Of , That is, it cannot be transferred
let name = { name: "123" }// FALSE , Do not redeposit
let ws = new WeakSet([name, { age: 321 }])
console.log(ws);The correct method should be directly in WeakSet You can use , Because if it is not used for a period of time, it will be recycled as garbage .
let ws = new WeakSet([{ name: "123" }, { age: 321 }])
console.log(ws);3、 ... and 、Map
Before we mentioned set It is inevitable to mention map, First of all, we have a simple understanding of them ,map It is mainly used for query .
Set and Map The main application scenario is Data reorganization and Data storage .
Set It's a data structure called a set ,Map It's a data structure called a dictionary .
aggregate And Dictionaries The difference between : Common ground : aggregate 、 Dictionaries Can store No repetition Value
Difference : aggregate In order to [value, value] The form of storage elements , Dictionaries In order to [key, value] In the form of storage
Map The way of use and Set Very similar ,size clear has delete,get set( Only these two are different )set Is the added value ,get Is to get the value
Four 、WeakMap
Turn the first value in the array into an object
let wm=new WeakMap([
[{name:"ww"},"xxx"]
])
console.log(wm);边栏推荐
- [Tang Laoshi] C -- encapsulation: member variables and access modifiers
- JVM JNI and PVM pybind11 mass data transmission and optimization
- How to handle wechat circle of friends marketing activities and share production and release skills
- Line segment tree blue book explanation + classic example acwing 1275 Maximum number
- 2022 safety officer-c certificate examination and safety officer-c certificate registration examination
- Fingerprint password lock based on Hal Library
- [raid] [simple DP] mine excavation
- Preliminary practice of niuke.com (11)
- 47. Process lock & process pool & Collaboration
- Apprentissage intensif - notes d'apprentissage 1 | concepts de base
猜你喜欢

Example of peanut shell inner net penetration

2.5 conversion of different data types (2)

2.2 integer

2.6 formula calculation
![Oak-d raspberry pie cloud project [with detailed code]](/img/34/76b461bf03fba373da5b5898c5204c.jpg)
Oak-d raspberry pie cloud project [with detailed code]

Based on laravel 5.5\5.6\5 X solution to the failure of installing laravel ide helper

19、 MySQL -- SQL statements and queries

From the behind the scenes arena of the ice and snow event, see how digital builders can ensure large-scale events

AcWing 1460. Where am i?

Interval product of zhinai sauce (prefix product + inverse element)
随机推荐
Based on laravel 5.5\5.6\5 X solution to the failure of installing laravel ide helper
How to modify the network IP addresses of mobile phones and computers?
Instructions for common methods of regular expressions
Global and Chinese market of high purity copper foil 2022-2028: Research Report on technology, participants, trends, market size and share
Reinforcement learning - learning notes 1 | basic concepts
Test access criteria
Thread, thread stack, method stack, the difference of creating thread
4. Data splitting of Flink real-time project
不同业务场景该如何选择缓存的读写策略?
Wargames study notes -- Leviathan
MDM mass data synchronization test verification
MySQL 8.0 data backup and recovery
Global and Chinese market of cyanuric acid 2022-2028: Research Report on technology, participants, trends, market size and share
How to handle wechat circle of friends marketing activities and share production and release skills
C 10 new feature [caller parameter expression] solves my confusion seven years ago
Change deepin to Alibaba image source
Use nodejs+express+mongodb to complete the data persistence project (with modified source code)
Plan for the first half of 2022 -- pass the PMP Exam
How can the outside world get values when using nodejs to link MySQL
Parental delegation mechanism