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

Introduction to the usage of Map in ES6 (code example)

不言
Release: 2018-11-14 16:16:59
forward
4326 people have browsed it

This article brings you an introduction to the usage of Map in ES6 (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Overview

Map is also a new data structure. It is actually often used in js. For example, in the chestnut below, we often use an object like this. Rather than saying that it is an object, it is actually More like a Map, but compared to a real Map, this is still a bit weak,

let color={
    "red":"#FF0000",
    "green":"#00FF00",
    "blue":"#0000FFF"
}
color['red']
Copy after login

Initialization

new Map([iterable])
Copy after login

Initializing a Map has an optional parameter, which must be an iterable Objects, iterable objects include String, Array, Array-Like obejct (Arguments, NodeList), Typed Array, Set, Map and user-defined iterable objects.

Array

new Map([[1,2],[3,4]]) // Map(2) {1 => 2, 3 => 4}
Copy after login

Add

Compared with the object as Map, the key of Map can be any value, even NaN

var myMap = new Map();
var keyObj = {},
    keyFunc = function () {},
    keyString = "a string";
// 添加键
myMap.set(keyString, "和键'a string'关联的值");
myMap.set(keyObj, "和键keyObj关联的值");
myMap.set(keyFunc, "和键keyFunc关联的值");
Copy after login

Get## The size of #Map
myMap.size    // 3
Copy after login

Get

myMap.get(keyString)   // "和键'a string'关联的值"
myMap.get(keyObj)     // "和键keyObj关联的值"
myMap.get(keyFunc)      // "和键keyFunc关联的值"
Copy after login

Whether it contains

myMap.has(keyString)  // true
myMap.has('1')  // false
Copy after login

Delete

myMap.delete(keyString)  // true
myMap.delete('')  // false
Copy after login

Traverse

myMap.forEach(m=>{console.log(m)})
// 和键'a string'关联的值
//  和键keyObj关联的值
//  和键keyFunc关联的值
Copy after login

Get the iterator

let entries=myMap.entries()
entries.next().value // 和键'a string'关联的值
entries.next().value//  和键keyObj关联的值
entries.next().value//   和键keyFunc关联的值
Copy after login

Get key iterator

let keys=myMap.keys()
keys.next().value //  "a string"
keys.next().value//  function () {}
keys.next().value//   {}
Copy after login

Get value iterator

let values=myMap.values()
values.next().value // 和键'a string'关联的值
values.next().value//  和键keyObj关联的值
values.next().value//   和键keyFunc关联的值
Copy after login

Clear

mySet.clear()
Copy after login


The above is the detailed content of Introduction to the usage of Map in ES6 (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
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!