The default object representation of JavaScript {} can be thought of as a map or dictionary data structure in other languages, a set of key-value pairs.
But the JavaScript object has a small problem, that is, the key must be a string , but actually number or other data type as a key is also very reasonable.
Map is the structure of a set of key-value pairs with extremely fast lookup speeds.
var m new Map ([[' Michael ', 95],[' Bob ', 75],[' Tracy ', []]); M.get (' Michael '); // the
Initializing a map requires a two-dimensional array, or directly initializing an empty map. Map has the following methods:
var New Map (); // Empty map M.set (' Adam ', 67); // Add a new Key-value M.set (' Bob ',); M.has (' Adam '); // is there a key ' Adam ': true M.get (' Adam '); // the M.delete(' Adam '); // Delete key ' Adam ' M.get (' Adam '); // undefined
There is rain a key can only correspond to one value, so, multiple times to a key put value, the following value will overwrite the front
var New Map (); M.set (' Adam ', (); M.set (' Adam ',N); M.get (' Adam '); // the
Set
Set is similar to a map, but it is also a set of keys, but does not store value, because key cannot be duplicated, so there is no duplicate key in set.
To create a set, you need to provide an array as input, or create an empty set directly:
var New Set (); // Empty Set var New Set ([n/a]); // contains
Repeating elements are automatically filtered in set:
var New Set ([1,2,3,3, ' 3 ']); s; // set{1,2,3, ' 3 '}, filters out a repeating element 3
You can add elements to the set by using the Add (key) method, but you can add them repeatedly, but it won't work.
>>> S.add (4) >>>s {1,2,3,4} >>> s.add (4) >>> s {1,2,3,4}
You can delete an element by using the Delete (key) method:
var New Set ([n/a]); s; // set{1,2,3} S.Delete(3); s; // set{1,2}
Map and set in JavaScript