Home > Web Front-end > JS Tutorial > body text

A first look at Map and WeakMap in ES6

不言
Release: 2018-03-31 10:35:35
Original
1543 people have browsed it

This article shares with you Map and WeakMap in ES6. I hope it can help friends in need

Map objects save key-value pairs. Any value (object or primitive) can be used as a key or a value.
Use mapping object
let myMap=new Map();
let keyObj={},keyfunc=function(){},keysTring='hello word';
Copy after login
set(key,value) Add value
myMap.set(keysTring,'keysTring的关联值')
myMap.set(keyfunc,'keyfunc的关联值')
myMap.set(keyObj,'keyObj的关联值')
Copy after login
size Get the size of Map
console.log(myMap.size)  //3
Copy after login
get(key) Get the size of map Value
console.log(myMap.get(keysTring)) //keysTring的关联值
console.log(myMap.get('hello word')) //keysTring的关联值


----------


console.log(myMap.get(keyfunc)) //keyfunc的关联值
console.log(function(){}===keyfunc)  //false 
console.log(myMap.get(function(){})) //undefined, 因为keyFunc !== function () {}


----------


console.log(myMap.get(keyObj)) //keyObj的关联值
console.log({}===keyObj)  //false 
console.log(myMap.get({})) //undefined 因为keyObj !== {}
Copy after login
Use NaN as the key of the map
myMap.set(NaN, "not a number");
console.log(myMap.get(NaN))  //not a number

var otherNaN=Number('foo');
console.log(otherNaN)  //NaN
console.log(myMap.get(otherNaN))  //not a number 也可以取值
Copy after login
The relationship between mapping and array object
let kvArray = [["key1", "value1"], ["key2", "value2"]];
var myMap1 = new Map(kvArray);
//使用映射对象常规的构造函数将一个二维键值对数组对象转换成一个映射关系
console.log(myMap1) //Map { 'key1' => 'value1', 'key2' => 'value2' }
console.log(myMap1.get('key1'))  //value1
console.log([...myMap1]) //[ [ 'key1', 'value1' ], [ 'key2', 'value2' ] ]
Copy after login
forEach()
var myMap2 = new Map(kvArray);
myMap2.forEach((value,index)=>{
    console.log(value+'---'+index)  //value1---key1   value2---key2
})
Copy after login
The for..of
var myMap3 = new Map(kvArray);
for(var a of myMap3){
    console.log(a)      //[ 'key1', 'value1' ] [ 'key2', 'value2' ]
}

for(var [key,value] of myMap3){
    console.log('key:'+key+', value:'+value)  //key:key1, value:value1 key:key2, value:value2
}
Copy after login
clear() method will remove all elements in the Map object.
let clearMap=new Map();
clearMap.set('hello','word');
console.log(clearMap.size)  //1
clearMap.clear();   //清空
console.log(clearMap.size)  //0
Copy after login
delete(key) is used to remove the specified element in the Map object.
let delMap=new Map();
delMap.set('hi','word');
delMap.set('hello','word');
console.log(delMap)  //Map { 'hi' => 'word', 'hello' => 'word' }
delMap.delete('hi'); //执行删除操作
console.log(delMap)  //Map { 'hello' => 'word' }
Copy after login
has(key) returns a bool value to indicate whether the specified element exists in the map.
let hasMap=new Map();
hasMap.set('hi','word');
hasMap.set('hi','hello word');
console.log(hasMap)  //Map { 'hi' => 'hello word' }   一样的key后面的value会覆盖前面的值
console.log(hasMap.has('hi')); //true
console.log(hasMap.has('hello')); //false
Copy after login
entries() converts the map object into an iterator
let entriesMap=new Map();
entriesMap.set('a','A');
entriesMap.set('b','B');
let entries=entriesMap.entries();
console.log(entries.next()); //{ value: [ 'a', 'A' ], done: false }
console.log(entries.next().value); //[ 'b', 'B' ]
console.log(entries.next()); //{ value: undefined, done: true }
Copy after login
values() returns a new Iterator object
let valueMap=new Map();
valueMap.set('a','A');
valueMap.set('b','B');
let values=valueMap.values();
console.log(values.next())  //{ value: 'A', done: false }
console.log(values.next())  //{ value: 'B', done: false }
console.log(values.next())  //{ value: undefined, done: true }
Copy after login
WeakMap object is a collection of key/value pairs, where the keys are weakreferences. The keys must be objects and the values ​​can be arbitrary.
WeakMap only provides 4 methods:
1.delete(key); 根据key删除value
2.get(key);根据key获取value
3.has(key) 根据key检查是不是存在value
4.set(key,value) 根据key设置value

let weakMap=new WeakMap();
weakMap.set('hi','word')  //异常   因为WeakMap的键只能是个对象
let obj1={};
weakMap.set(obj1,'word')  //正常
console.log(weakMap)   //WeakMap {}
console.log(weakMap.get(obj1))  //word
console.log(weakMap.has(obj1))  //true
console.log(weakMap.delete(obj1))  //true
Copy after login

Related recommendations:

Introducing in detail the new features of ES6 - code examples of Map and WeakMap objects in JavaScript



The above is the detailed content of A first look at Map and WeakMap in ES6. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!