ES6 learning tutorials-Summary of common Map methods, es6 tutorial map summary

Source: Internet
Author: User
Tags es6 tutorial

ES6 learning tutorials-Summary of common Map methods, es6 tutorial map summary

Preface

ES6 contains many new language features, which makes JavaScript more powerful and expressive. This article will give you a detailed description of the common Map methods in ES6. Let's take a look at the detailed introduction:

1. Convert the Map structure into an array structure

A quick way is to use the extension operator (...) in combination (...)

let map = new Map([ [1, 'one'], [2, 'two'], [3, 'three'],]);[...map.keys()]// [1, 2, 3][...map.values()]// ['one', 'two', 'three'][...map.entries()]// [[1,'one'], [2, 'two'], [3, 'three']][...map]// [[1,'one'], [2, 'two'], [3, 'three']]

2. Map Traversal

Map native provides three traversal servers:

  • Keys (): The traversal tool of the Return key name.
  • Values (): The iterator that returns the key value.
  • Entries (): returns the traversal of all members.

The following is an instance.

Let map = new Map ([['F', 'no'], ['T', 'yes'],]); for (let key of map. keys () {console. log (key);} // "F" // "T" for (let value of map. values () {console. log (value);} // "no" // "yes" for (let item of map. entries () {console. log (item [0], item [1]);} // "F" "no" // "T" "yes" // or for (let [key, value] of map. entries () {console. log (key, value);} // equivalent to map. entries () for (let [key, value] of map) {console. log (key, value );}

The example at the end of the code above indicates that the default traversal interface (Symbol. iterator attribute) of the Map structure is the entries method.

map[Symbol.iterator] === map.entries // true

3. Map get Length

map.size;

4. Map to obtain the first element

const m = new Map();m.set('key1', {})m.set('keyN', {})console.log(m.entries().next().value); // [ 'key1', {} ]

Obtain the first key

console.log(m.keys().next().value); // key1

Obtain the first value

console.log(m.values().next().value); // {}

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

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.