当前位置:网站首页>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);边栏推荐
- Phpexcel import export
- Offset related concepts + drag modal box case
- 47. Process lock & process pool & Collaboration
- How to set the system volume programmatically- How to programmatically set the system volume?
- 9 pyqt5 qscrollarea scroll area and qscrollbar scroll bar
- MySQL learning notes - single table query
- Golang type assertion and conversion (and strconv package)
- Node MySQL serialize cannot rollback transactions
- 阻塞非阻塞和同步异步的区分 参考一些书籍
- IP address is such an important knowledge that it's useless to listen to a younger student?
猜你喜欢

In 2021, the global foam protection packaging revenue was about $5286.7 million, and it is expected to reach $6615 million in 2028

LabVIEW training

Explore the internal mechanism of modern browsers (I) (original translation)

jvm jni 及 pvm pybind11 大批量数据传输及优化
![C 10 new feature [caller parameter expression] solves my confusion seven years ago](/img/32/2d81237d4f1165f710a27a7c4eb1e1.jpg)
C 10 new feature [caller parameter expression] solves my confusion seven years ago

String and+

Vscode reports an error according to the go plug-in go get connectex: a connection attempt failed because the connected party did not pro

Camera calibration (I): robot hand eye calibration

Sightseeing - statistics of the number of shortest paths + state transfer + secondary small paths

JVM JNI and PVM pybind11 mass data transmission and optimization
随机推荐
AI enhanced safety monitoring project [with detailed code]
9 pyqt5 qscrollarea scroll area and qscrollbar scroll bar
Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles
MySQL learning notes - single table query
Global and Chinese market of liquid antifreeze 2022-2028: Research Report on technology, participants, trends, market size and share
Do you really know how old you are?
Sightseeing - statistics of the number of shortest paths + state transfer + secondary small paths
JVM JNI and PVM pybind11 mass data transmission and optimization
Global and Chinese markets of lithium chloride 2022-2028: Research Report on technology, participants, trends, market size and share
Instructions for common methods of regular expressions
Blue Bridge Cup: the fourth preliminary - "simulated intelligent irrigation system"
AST (Abstract Syntax Tree)
2.2 integer
[Tang Laoshi] C -- encapsulation: member variables and access modifiers
Global and Chinese markets of active matrix LCD 2022-2028: Research Report on technology, participants, trends, market size and share
Preliminary practice of niuke.com (11)
4. Data splitting of Flink real-time project
Parental delegation mechanism
Machine learning support vector machine SVM
Qt6 QML Book/Qt Quick 3D/基础知识