当前位置:网站首页>ES6's new data container map
ES6's new data container map
2022-07-28 19:23:00 【ca11meback】
map No grammar sugar
There are only standard creation statements
var map1=new Map([["age",100],[20,"hello"],[a,90]])// initialization 
Something like this is map data
The parameter passed in is an array , There are three data in the array , The first three data are key The back is value
key Can be any type of data It can be said to be basic type data , It can also be reference data
var re1=map1.get("age")
var re2=map1.get(20)
var re3=map1.get(["key"])
var re4=map1.get(a)
console.log(re1,re2,re3,re4)Use get Function to get data
!!! The problem that needs attention is : We can't be like re3 Take like this , Because this is not the same reference , This is a re created reference , We can only access this data through the same reference
map1.set("life",1)
map1.set("age",1)
console.log(map1)
console.log(map1.size)
You can see that the new value overrides the old value , If map There is this in the data key Then the new value will overwrite , Add if not
Use set Function to store data
map1.set("life",1)
map1.set("age",1)
console.log(map1)
console.log(map1.size)
map1.delete(20)
var re5=map1.delete("age")
var re6=map1.delete("age")
console.log(map1,re5,re6) ![]()
You can see the use of delete The return value of the function is a Boolean value
map1.clear()
console.log(map1)clear The function is used to empty the array
Take a large example
var mycar = {
data: new Map(),// data source The data source passed in from the back end is converted into what we want to use map data
clear:function(){// Empty function
this.data.clear()
},
jianshao:function(id){// Reduction function The idea is based on count Cannot be less than or equal to 0 Then click one to call the primary function According to the dishes id Find the data Reduce count Number
let obj=this.data.get(id)
if(obj){
obj.count--
if(obj.count<=0){
this.data.delete(id)
}
}
},
add: function(id) {// Add function Similar to the reduction function, but the first thing we need to judge is whether the dish exists
let obj = this.data.get(id)
if (obj) {
// console.log(obj)
obj.count++
this.data.set(id, obj)
}
},
total: function() {// Total price Traversal data
var result = 0;
this.data.forEach(function(el) {
console.log(el)
result += el.count * el.price
})
return result
},
network: function() {// Fake data
var jiashuju = [{
id: 435123,
title: " Yu-Shiang Shredded Pork ",
price: 18,
count: 1
}, {
id: 435124,
title: " Dongpo's braised pork ",
price: 28,
count: 1
}, {
id: 435125,
title: " Steamed Rice ",
price: 2,
count: 2
}];
this.data.set(jiashuju[0].id,jiashuju[0])
this.data.set(jiashuju[1].id,jiashuju[1])
this.data.set(jiashuju[2].id,jiashuju[2])
var self = this
jiashuju.map(function(el) {
self.data.set(el.id, el)
})
}
}
mycar.network()
mycar.add(435123)
mycar.add(435125)
mycar.add(435125)
mycar.add(435125)
mycar.add(435125)
mycar.add(435125)
mycar.jianshao(435124)
console.log(mycar.data)
var re1 = mycar.total()
console.log(re1)
mycar.clear()
console.log(mycar.data)边栏推荐
- Fundamentals of software testing and development | practical development of several tools in testing and development
- 【物理应用】大气吸收损耗附matlab代码
- [physical application] atmospheric absorption loss with matlab code
- RTC clock: a calendar
- BLDC 6步换相 simulink
- Applet applet jump to official account page
- 剑指 Offer II 109. 开密码锁
- 智能合约安全——溢出漏洞
- [data analysis] realize SVDD decision boundary visualization based on MATLAB
- RFs self study notes (4): actual measurement model - the mixture of OK and CK, and then calculate the likelihood probability
猜你喜欢
![[radar] radar signal online sorting based on kernel clustering with matlab code](/img/56/1f8e8690b47fc4a1f101d4e530b87f.png)
[radar] radar signal online sorting based on kernel clustering with matlab code

Adobe Flash player 34.0.0.92 and available version modification methods (2021-01-23

2022年暑假ACM热身练习3(详细)

BM14 链表的奇偶重排

Srs4.0 installation steps

Fantasy 5 (ue5) game engine complete course 2022

【图像分割】基于方向谷形检测实现静脉纹路分割附MATLAB代码

QT widget promoted to QWidget

Solve the critical path in FJSP - with Matlab source code

Qt: one signal binds multiple slots
随机推荐
Pytorch:快速求得NxN矩阵的主对角线(diagonal)元素与非对角线元素
QT user defined control user guide (flying Qingyun)
Getting started with QT & OpenGL
Gmoea code operation 2 -- establishment and operation of operation environment
【数据分析】基于MATLAB实现SVDD决策边界可视化
Efficiency comparison of JS array splicing push() concat() methods
How to obtain data on mobile phones and web pages after the SCM data is uploaded to Alibaba cloud Internet of things platform?
The difference between --save Dev and --save in NPM
SQL custom automatic calculation
【滤波跟踪】基于EKF、时差和频差定位实现目标跟踪附matlab代码
Structure and working principle of thyristor
Pandownload revival tutorial
Qt: one signal binds multiple slots
RTC clock: a calendar
优麒麟系统安装BeyondComare
GPIO port configuration of K60
Application of time series database in cigarette factory
BM16 删除有序链表中重复的元素-II
2022年中总结
R语言与数据分析实战11-数据的删除