2. Data Structure map.html

Source: Internet
Author: User
Tags map class map data structure

Map is a dictionary data structure provided by ES.

Dictionary structure--used to store hash structures that do not duplicate keys. Unlike collection (set), a dictionary uses key-value pairs to store data

JavaScript objects (object:{}) can only be used as a string for key, which is inconvenient for use
To solve this problem, ES6 provides a map data structure. It is similar to an object and is also a collection of key-value pairs, but the scope of "key" is not limited to strings, but values of various types can be used as keys. That is, object provides a "string-value" of the corresponding structure, MAP provides a "value-value" of the corresponding. is a more perfect hash structure.

Object Support demo for the map structure
var data1={a:1},      data2={b:2},      obj={};       // adding properties for obj objects (DATA1 and data2 as property names)      Obj[data1]=1;      OBJ[DATA2]=2;      Console.log (obj);
You can see the results are not what we need.

Create a map
Const map=new  map ([     [' a ', 1],     [' B ', 2]]); Console.log (map);
The above [' a ', 1] stands for a as key,1 as the value declaration
Is it possible to write a third element in [' A ', 1]? The answer is yes, but! ES6 won't talk to you.

The attribute of the map class--size is     used to get the length of the map
Console.log (map.size);
The method of the map class--set   Map.set (key,value) sets the key name key corresponding to value  , and then returns the entire map structure. If key already has a value, the key value is updated, otherwise a new key is generated
Console.log (Map.set ([3], ' array of elements ')); // You can also add map.set (' name ', ' Zhang San ') to the chain. Set (' Age ', '); Console.log (map) ;
The method of the Map class--get  map.get (key)    reads key values corresponding to keys and returns undefined if not obtained
Console.log (Map.get (' name '));
Gets the corresponding key value of the array--subsequent additions
Map class method--delete   map.delete (key)     deletes a key if the delete succeeds returns true, otherwise false
Console.log (map.  Delete(' name '));
Method of the Map class--has      Map.has (key)        determines whether a key exists in the map, returns a Boolean value
Console.log (Map.has (' age '));
Method of Map class--clear    map.clear ()         clears all data, no return value
Map.clear ();
Method of the Map class--keys     Map.keys ()          returns the key name of the walker
Console.log (Map.keys ());
Method of the Map class--values   map.values ()        The iterator that returns the key value
Console.log (Map.values ());
Method of the Map class--entries  map.entries ()       a walker that returns a key-value pair
Console.log (Map.entries ());
Method of the Map class--foreach  Map.foreach (function () {})   iterates through each member using a callback
Map.foreach (function  (value, key, map) {      Console.log (' This is Key:${key}, this is Value:${value}, this is the map itself: ${map }`);})
Map in the use of the process need to pay attention to the place
1. In JS, Nan is not equal to its own data type, but in the map, set the data with Nan as key, it will produce the behavior of overwriting. Indicates that map treats Nan as the same key
Map.set (nan,1). Set (nan,10); Console.log (map);
2. If a null object is used as key ({}) in the map data, two keys appear, because each {} is a completely new reference, different from the memory address. In the case of the above Nan, there is only one address, so there will be coverage.
Map.set ({},1). Set ({},2); Console.log (map);

2. Data Structure map.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.