ES6 New Data Structure Map function and usage example, es6map

Source: Internet
Author: User

ES6 New Data Structure Map function and usage example, es6map

This example describes the functions and usage of ES6's new data structure Map. We will share this with you for your reference. The details are as follows:

New data structure Map

In Javascript, objects are essentially a set of key-value pairs, but keys can only be strings. To make up for this defect, ES6 brings a new data structure Map. Map is also a set of key-value pairs, except that keys can be strings or other data types, such as objects (is it amazing ). See the following example.

var m = new Map();var ul = document.getElementsByTagName('ul');m.set(ul,'hi');console.log(m.get(ul)); //hi

Map operations include the following:

M. set (ul, 'content'); // Add member m to Map. get (ul); // obtain the value m corresponding to the ul key. has (ul); // returns a Boolean value to determine whether the key ulm exists. delete (ul); // delete the key ul. true is returned for success, and falsem is returned for failure. size // return m length. clear (); // clear all m members

Method for assigning an initial value to Map:

var m = new Map([[li_1,'hello'],[li_2,'world']]);

The accepted parameter is an array. The members in the array are arrays that represent one key-value pair. If you are blind, check the actual execution of the upstream code:

var li_1 = document.getElementsByTagName('li')[0];var li_2 = document.getElementsByTagName('li')[1];var list = [ [li_1,'hello'] , [li_2,'world'] ];var m = new Map();list.forEach( ([key,value]) => m.set(key,value) );console.log(m.get(li_1)); //hello

If you assign values to one key multiple times, the subsequent values overwrite the previous values. It is worth noting that when the key is an object, it must be referenced in the same way to think that the key is the same. The following describes the Map Traversal method.

Var ul = document. getElementsByTagName ('ul '); var li_1 = document. getElementsByTagName ('lil') [0]; var li_2 = document. getElementsByTagName ('lil') [1]; var list = [[li_1, 'Hello'], [li_2, 'World']; var m = new Map (); list. forEach ([key, value]) => m. set (key, value); for (let key of m. keys () {console. log (key) ;}for (let val of m. values () {console. log (val) ;}for (let item of m. entries () {console. log (item [0], item [1]);} for (let [key, val] of m. entries () {console. log (key, val);} m. forEach (function (val, key, ul) {console. log (this); // ul console. log (val, key) ;}, ul); // The second parameter of forEach is used to change the value this points

Conversion between Map and other data types

The simplest way for Map to convert data is to use the... extension operator. For example:

console.log( ...m ); //[li, "hello"] [li, "world"]

When Map is used to convert objects, all keys must be strings and the Object. create () function is used. When Map is converted to JSON, all keys must be strings and the JSON. Stringify () function is used.

I hope this article will help you design the ECMAscript program.

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.