Map

A Map is a collection of key/value pairs. Keys and values of a Map can be of any data type. Key/value pairs are arranged in insertion order. A Map object is created using the Map constructor.

Here is an example, which demonstrates how to create a Map object and do various operations on it:

let map = new Map();let o = {n: 1};map.set(o, "A"); //addmap.set("2", 9);console.log(map.has("2")); //check if key existsconsole.log(map.get(o)); //retrieve value associated with keyconsole.log(...map);map.delete("2"); //delete key and associated valuemap.clear(); //delete everything//create a map from iterable objectlet map_1 = new Map([[1, 2], [4, 5]]);console.log(map_1.size); //number of keys

The output is as follows:

trueA[object Object],A 2,9

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.