当前位置:网站首页>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]);
边栏推荐
- 适配器模式(Adapter)
- Sinotech software outsourcing
- Custom encapsulation drop-down component
- POJ 3070 Fibonacci
- Coprime and non coprime of template problems of Chinese remainder theorem
- C language: Exercise 2
- Enjoy element mode (flyweight)
- Oplg: new generation cloud native observable best practices
- Design of four kinds of linear phase FIR filters -- complete set of Matlab source code
- 2. Introduction to parallel interface, protocol and related chips (8080, 8060)
猜你喜欢
随机推荐
MySQL讲解(二)
D - skiing
橋接模式(Bridge)
[how to connect the network] Chapter 2 (next): receiving a network packet
Basic methods for network diagnosis and hardware troubleshooting of Beifu EtherCAT module
Use the script to crawl the beautiful sentences of the sentence fan website and store them locally (blessed are those who like to excerpt!)
G - Cow Bowling
MariaDB study notes
F - Charm Bracelet
[how to connect the network] Chapter 1: the browser generates messages
Processsing mouse interactive learning
C language: Exercise 2
原型模式(prototype)
10秒内完成火灾预警,百度智能云助力昆明官渡打造智慧城市新标杆
Hdu1724[Simpson formula for integral]ellipse
Chapter 10 setting up structured logging (2)
2、并行接口、协议和相关芯片介绍(8080、8060)
Digital signal processing -- Design of linear phase type (Ⅰ, Ⅲ) FIR filter (1)
B - Bridging signals
Word document export (using fixed template)








![Hdu1724[Simpson formula for integral]ellipse](/img/57/fb5098e150b5f3d91a5d0983a336ee.png)
