Home > Web Front-end > JS Tutorial > body text

Summary of common methods of Map

巴扎黑
Release: 2017-08-13 14:47:44
Original
1682 people have browsed it

Map is a new data structure in ES6. It is added together with Set. In fact, the functions are similar. The following article mainly summarizes and introduces the common methods of Map in ES6 learning tutorials. The article introduces it in detail through sample code. Friends in need can refer to it. Let’s take a look together.

Preface

ES6 contains many new language features that will make JS more powerful and expressive. This article will give you a detailed introduction to the common methods of Map in ES6. Without further ado, let’s take a look at the detailed introduction:

1. Convert Map structure to array structure

A faster method is to use the spread operator (...)


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']]
Copy after login

2 .Map loop traversal

Map natively provides three traversers:

  • keys(): a traverser that returns key names.

  • values(): Returns a traverser of key values.

  • entries(): Returns a traverser of all members.

The following are usage examples.


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"

// 或者
for (let [key, value] of map.entries()) {
 console.log(key, value);
}

// 等同于使用map.entries()
for (let [key, value] of map) {
 console.log(key, value);
}
Copy after login

The last example of the above code represents the default iterator interface (Symbol.iterator attribute) of the Map structure, which is the entries method.


map[Symbol.iterator] === map.entries // true
Copy after login

3.Map Get the length


map.size;
Copy after login

4.Map Get the first element


##

const m = new Map();
m.set('key1', {})
m.set('keyN', {})

console.log(m.entries().next().value); // [ 'key1', {} ]
Copy after login

Get the first key


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

Get the first value


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

The above is the detailed content of Summary of common methods of Map. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!