当前位置:网站首页>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);边栏推荐
- JVM JNI and PVM pybind11 mass data transmission and optimization
- Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of rotary tablet presses in the global market in 2022
- [Tang Laoshi] C -- encapsulation: member variables and access modifiers
- Global and Chinese market of high temperature Silver sintering paste 2022-2028: Research Report on technology, participants, trends, market size and share
- Micro service knowledge sorting - cache technology
- 2.1 use of variables
- Global and Chinese market of full authority digital engine control (FADEC) 2022-2028: Research Report on technology, participants, trends, market size and share
- SQL injection - Fundamentals of SQL database operation
- Viewing Chinese science and technology from the Winter Olympics (II): when snowmaking breakthrough is in progress
- Class loading process
猜你喜欢

Change deepin to Alibaba image source

2.3 other data types

Qtablewidget control of QT

String and+

Commands related to files and directories

Gym welcomes the first complete environmental document, which makes it easier to get started with intensive learning!

2022 high voltage electrician examination and high voltage electrician reexamination examination

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

不同业务场景该如何选择缓存的读写策略?

Operate BOM objects (key)
随机推荐
Plan for the first half of 2022 -- pass the PMP Exam
Battle drag method 1: moderately optimistic, build self-confidence (1)
Node MySQL serialize cannot rollback transactions
Initialization and instantiation
First knowledge of database
Global and Chinese markets of lithium chloride 2022-2028: Research Report on technology, participants, trends, market size and share
AcWing 1460. Where am i?
Print linked list from end to end
2022 low voltage electrician examination and low voltage electrician simulation examination question bank
【c】 Digital bomb
2022 safety officer-c certificate examination and safety officer-c certificate registration examination
2022 high voltage electrician examination and high voltage electrician reexamination examination
Q&A:Transformer, Bert, ELMO, GPT, VIT
Example of peanut shell inner net penetration
Wargames study notes -- Leviathan
SQL injection - Fundamentals of SQL database operation
4. Data splitting of Flink real-time project
Commands related to files and directories
11-grom-v2-04-advanced query
Cannot load driver class: com. mysql. cj. jdbc. Driver