Home  >  Article  >  Web Front-end  >  Detailed explanation of Map and common API examples

Detailed explanation of Map and common API examples

小云云
小云云Original
2018-02-01 13:36:061568browse

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 forced type conversion will occur. This article mainly brings you an es6 series tutorial_ Map detailed explanation and common API introduction.

How to use Map?

let map = new Map();

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;
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

You can use objects as the keys of the Map. Although they are two empty objects, strong type conversion will not occur.

has( key ): Determine whether the key name exists

delete( key ): Delete the key name and the corresponding value

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

Map supports array initialization, using a two-dimensional array, and 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

Map also Supports the forEach method, supports 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 );

Related recommendations:

javascript common api

Detailed introduction to the summary of commonly used APIs for JavaScript operations on DOM

Detailed explanation of html5 canvas common API summary (2)--Drawing API

The above is the detailed content of Detailed explanation of Map and common API examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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