Home > Web Front-end > JS Tutorial > Detailed explanation of Map and introduction to commonly used APIs

Detailed explanation of Map and introduction to commonly used APIs

巴扎黑
Release: 2017-09-26 09:39:50
Original
1660 people have browsed it

The following editor will bring you an es6 series tutorial_Detailed explanation of Map and introduction to commonly used APIs. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor to take a look

The Map type in ECMAScript 6 is an ordered list that stores many key-value pairs. Key-value pairs support all data types. Keys 0 and '0' will be treated as two different keys, and no cast conversion will occur.

How to use Map?


let map = new Map();
Copy after login

Common methods:

set(key, value): Add a new key-value pair element

get(key): Get the value corresponding to the key. If the value does not exist, return undefined


let map = new Map();
map.set( '0', 'ghostwu' );
map.set( 0, 'ghostwu' );

console.log( map.get( '0' ) ); //ghostwu
console.log( map.get( 'name' ) ); //undefined;
Copy after login


let map = new Map();
var key1 = {}, key2 = {};

map.set( key1, 'ghostwu' );
map.set( key2, 22 );

console.log( map.get( key1 ) ); //ghostwu
console.log( map.get( key2 ) ); //22
Copy after login

Okay Use the object as the key of the Map. Although there are two empty objects, strong type conversion will not occur.

has( key): Determine whether the key name exists

delete( key): Delete key names and corresponding values

clear(): Remove all key-value pairs in the map collection

size: The number of elements in the map collection


let map = new Map();
map.set( 'name', 'ghostwu' );
map.set( 'age', 22 );

console.log( map.has( 'name' ) );//true
console.log( map.size ); //2

map.delete( 'name' );
console.log( map.has( 'name' ) );//false
console.log( map.size ); //1
console.log( map.has( 'age' ) ); //true

map.clear();
console.log( map.size ); //0
console.log( map.has( 'age' ) ); //false
Copy after login

Map supports array initialization, using a two-dimensional array, each array uses key-value pairs


let map = new Map( [ [ 'name', 'ghostwu' ], [ 'age', 22 ] ] );
console.log( map.has( 'name') ); //true
console.log( map.has( 'age') ); //true
console.log( map.size ); //2
map.set( 'sex', 'man' );
console.log( map.size );
console.log( map.get( 'name' ) ); //ghostwu
map.clear();
console.log( map.size ); //0
Copy after login

Map also supports the forEach method, support 2 parameters, the first one: function, the function supports 3 parameters (value, key, current map), the second one: this


let map = new Map( [ [ 'name', 'ghostwu' ], [ 'age', 22 ] ] );
map.set( 'sex', 'man' );
map.forEach( function( val, key, cur ){
 console.log( val, key, cur, this );
}, 100 );
Copy after login

The above is the detailed content of Detailed explanation of Map and introduction to commonly used APIs. 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