当前位置:网站首页>ES6:Map
ES6:Map
2022-06-26 13:18:00 【HaanLen】
Map object
1、Map object
Map Object to save key value pairs . Any value ( Object or original value ) Can be used as a key or as a value .
2、Maps and Objects The difference between
- One Object The key of can only be a string or Symbols, But a Map The bond of can be any value .
- Map The key values in are ordered (FIFO principle ), The key added to the object is not .
- Map The number of key value pairs can be obtained from size Property acquisition , and Object The number of key value pairs can only be calculated manually .
- Object They all have their own prototypes , The key name on the prototype chain may conflict with the key name you set on the object .
3、Map Medium key
(1)key Is string
var myMap=new Map();
var kk='a string';
myMap.set(kk,"for a thing");
console.log(myMap.get(kk))//for a thing
console.log(myMap.get("a string"))//for a thing
(2)key It's the object
var myMap=new Map();
var keyObj = {
}
myMap.set(keyObj,"for a thing");
console.log(myMap.get(keyObj))//for a thing
console.log(myMap.get({
}))//undefined
(3)key Is the function
var myMap=new Map();
var keyFunc=function () {
}
myMap.set(keyFunc,"for a thing");
console.log(myMap.get(keyFunc))//for a thing
console.log(myMap.get(function(){
}))//undefined
(4)key yes NaN
var myMap=new Map();
myMap.set(NaN,"for a thing");
console.log(myMap.get(NaN))//for a thing
var otherNaN=Number("you")
console.log(myMap.get(otherNaN))//for a thing
var myMap=new Map();
myMap.set(NaN,"for a thing");
console.log(myMap.get(NaN))//for a thing
var otherNaN=Number(2,4)
console.log(myMap.get(otherNaN))//undefined
4、Map Iteration
var myMap=new Map();
myMap.set(0,"def")
myMap.set(1,"rfg")myMap.set(3,"ppp")
for(var [index,value] of myMap){
console.log(index+"="+value)
}
var myMap=new Map();
myMap.set(0,"def")
myMap.set(1,"rfg")myMap.set(3,"ppp")
for(var [index,value] of myMap.entries()){
console.log(index+"="+value)
}
This entries Method returns a new one Iterator object , It contains... In insertion order Map Of each element in the object [index, value] Array .
var myMap=new Map();
myMap.set(0,"def")
myMap.set(1,"rfg")
myMap.set(3,"ppp")
for (var key of myMap.keys()) {
console.log(key);
}
This keys Method returns a new one Iterator object , It contains... In insertion order Map The key of each element in the object .
var myMap=new Map();
myMap.set(0,"def")
myMap.set(1,"rfg")
myMap.set(3,"ppp")
for (var value of myMap.values()) {
console.log(value);
}
This values Method returns a new one Iterator object , It contains... In insertion order Map The value of each element in the object .
var myMap=new Map();
myMap.set(0,"def")
myMap.set(1,"rfg")
myMap.set(3,"ppp")
myMap.forEach(function(value,key){
console.log(key+"="+value);
},myMap);

5、Map Operations on objects
(1)Map And Array Transformation
var k=[["one","thr"],["two","yht"]]
var myMap=new Map(k);
var arr=Array.from(myMap)
console.log(arr)
Map A constructor can A two-dimensional The key value is converted to an array Map Object use Array.from Function can Map Object to an array of two-dimensional key value pairs 
(2)Map The clone
var myMap1 = new Map([["key1", "value1"], ["key2", "value2"]]);
var myMap2 = new Map(myMap1);
console.log(myMap2===myMap1)//false
//Map The object constructor generates an instance , Iterate over new objects .
console.log(myMap2)

(3)Map The merger of
var first = new Map([[1, 'one'], [2, 'two'], [3, 'three'],]);
var second = new Map([[1, 'uno'], [2, 'dos']]);
// Merge two Map Object time , If there are duplicate key values , Then the latter will cover the former , The corresponding value is uno,dos, three
var merged = new Map([...first, ...second]);
边栏推荐
- Go structure method
- Do you know the limitations of automated testing?
- Zoomeeper sets ACL permission control (only specific IP access is allowed to enhance security)
- Beifu PLC model selection -- how to see whether the motor is a multi turn absolute value encoder or a single turn absolute value encoder
- Use the script to crawl the beautiful sentences of the sentence fan website and store them locally (blessed are those who like to excerpt!)
- Detailed explanation of C const: definition and use of C constant
- Electron official docs series: Distribution
- MySQL数据库讲解(四)
- PostGIS geographic function
- C - Common Subsequence
猜你喜欢

【Spark】. Explanation of several icons of scala file in idea

MySQL数据库讲解(三)
![Vivado error code [drc pdcn-2721] resolution](/img/de/ce1a72f072254ae227fdcb307641a2.png)
Vivado error code [drc pdcn-2721] resolution

Beifu PLC realizes zero point power-off hold of absolute value encoder -- use of bias

2、并行接口、协议和相关芯片介绍(8080、8060)

倍福TwinCAT3实现CSV、TXT文件读写操作

mariadb学习笔记

Basic configuration and test of Beifu twincat3 NCI in NC axis interface

利用scrapy爬取句子迷网站优美句子存储到本地(喜欢摘抄的人有福了!)

Detailed explanation of C const: definition and use of C constant
随机推荐
HDU 3709 Balanced Number
Map value
I have a good word to say, and I admire myself
Update and download of Beifu EtherCAT XML description file
HDU1724[辛普森公式求积分]Ellipse
Sinotech software outsourcing
中国剩余定理模板题 互质与非互质
Word document export (using fixed template)
Copy multiple Excel files and name them different
What should the software test report include? Interview must ask
Bifu divides EtherCAT module into multiple synchronization units for operation -- use of sync units
橋接模式(Bridge)
2. Introduction to parallel interface, protocol and related chips (8080, 8060)
zoopeeper设置acl权限控制(只允许特定ip访问,加强安全)
C structure: definition and example
SQL assigns the field value of data table B to a column in data table a
I - Dollar Dayz
POJ 3070 Fibonacci
Bridge mode
解中小企业之困,百度智能云打个样