当前位置:网站首页>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);
边栏推荐
- Example of peanut shell inner net penetration
- Pytorch sets the weight and bias of the model to zero
- MySQL dump - exclude some table data - MySQL dump - exclude some table data
- Global and Chinese market of speed limiter 2022-2028: Research Report on technology, participants, trends, market size and share
- Camera calibration (I): robot hand eye calibration
- Microservice knowledge sorting - search technology and automatic deployment technology
- Thread, thread stack, method stack, the difference of creating thread
- It is discussed that the success of Vit lies not in attention. Shiftvit uses the precision of swing transformer to outperform the speed of RESNET
- Day6 merge two ordered arrays
- How to improve data security by renting servers in Hong Kong
猜你喜欢
Qtablewidget control of QT
jvm jni 及 pvm pybind11 大批量数据传输及优化
Explore the internal mechanism of modern browsers (I) (original translation)
Popularize the basics of IP routing
An old programmer gave it to college students
Shortest path problem of graph theory (acwing template)
JMeter plug-in installation
MySQL master-slave synchronization principle
2022 melting welding and thermal cutting examination materials and free melting welding and thermal cutting examination questions
1.5 learn to find mistakes first
随机推荐
Assign the CMD command execution result to a variable
How to improve data security by renting servers in Hong Kong
Microservice framework - frequently asked questions
[Tang Laoshi] C -- encapsulation: member variables and access modifiers
Global and Chinese markets of cast iron diaphragm valves 2022-2028: Research Report on technology, participants, trends, market size and share
Micro service knowledge sorting - asynchronous communication technology
Test panghu was teaching you how to use the technical code to flirt with girls online on Valentine's Day 520
Shortest path problem of graph theory (acwing template)
Derivation of decision tree theory
Pytorch sets the weight and bias of the model to zero
Viewing Chinese science and technology from the Winter Olympics (II): when snowmaking breakthrough is in progress
Sparse matrix (triple) creation, transpose, traversal, addition, subtraction, multiplication. C implementation
jvm jni 及 pvm pybind11 大批量数据传输及优化
Commands related to files and directories
JVM JNI and PVM pybind11 mass data transmission and optimization
Qt6 QML Book/Qt Quick 3D/基础知识
[effective Objective-C] - block and grand central distribution
P5.js development - setting
如临现场的视觉感染力,NBA决赛直播还能这样看?
Change deepin to Alibaba image source