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.