ES6 series tutorial _ MAP detailed explanation and common API introduction

The MAP type in ECMAScript 6 is an ordered list of monologous key values. Key value pairs support all data types. Keys 0 and ‘0’ will be used as two different keys, and there will be no forced type conversion.

How to use Map?

Let MAP = New Map ();
  Common methods:  
Set (key, value): Add a new key value to the element

Get: Get the value corresponding to the key, if this value does not exist, return undefined

let map = new map (); map.set (‘0’, ‘ghostwu); map.set (0,’ ghostwu ‘); console.log (map.get (‘ 0 ‘)); //ghostwuconsole.log (map.get (‘name’)); // Undefined;

 Let map = new map ); var key1 = {}, key2 = {}; map.set (key1, 'ghostwu'); map.set (KEY2, 22); console.log (map.get (key1)); //ghostwuconsole.log (Map.get (key2)); // 22   You can use the object as a key. Although it is two empty objects, strong type conversion will not occur. 
HAS (KEY): Judging whether the key name exists
Delete (key): Delete the key name and the corresponding value
clear (): Remove all key value pairs in the MAP collection
  Size: Map collection Elements  
Let map = new map (); map.set (‘Name’, ‘ghostwu’); map.set (‘agn’, 22); console.log Map.has (‘name’)); // trueconsole.log (map.size); //2map.delete (‘name’); console.log (map.has (‘name’)); // falseconsole. Log (map.size); //1console.log (map.has (‘agn’)); //truemap.clear (); console.log (map.size); //0console.log (map.has ‘Age’)); // False

MAP supports array initialization, with a two-dimensional array, however, for each array value pair

Let map = new map ([[‘Name’, ‘ghostwu’], [‘Age’, 22]]); console.log (map.has (‘name’)); //trueconsole.log (map.has (‘agn’)); //trueconsole.log (map.size); //2map.set (‘sex’, ‘man’); console.log (map.size) Console.logap.get (‘name’); //gHostwumap.clear (); console.log (map.size); // 0

Map also supports the foreach method Support 2 parameters, the first: function, the function support 3 parameters (value, keys, current map), the second: this
   let map = New Map ([['Name', 'GhostWu'], ['Age', 22]]); Map.Set ('SEX', 'MAN'); Map.Foreach (Function (VAL, KEY, CUR) { Console.log (Val, Key, Cur, this);}, 100); 

This ES6 series tutorial _ map Detailed explanation and common API introduction is the full content of Xiaobian sharing, I hope to give you a reference, I hope everyone will support Tumi Cloud.
© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment