SoFunction
Updated on 2025-03-06

JavaScript Map and Set_Power Node Java Academy Organized

JavaScript's default object representation{}Can be regarded as in other languagesMaporDictionarydata structure, that is, a set of key-value pairs.
But there is a small problem with JavaScript objects, that is, the key must be a string. But in fact, Number or other data types are also very reasonable as keys.
To solve this problem, the latest ES6 specification introduces a new data type Map. To test whether your browser supports ES6 specifications, please execute the following code. If the browser reports a ReferenceError error, you need to change to a browser that supports ES6:

'use strict';
var m = new Map();
var s = new Set();
alert('Your browser supports Map and Set!  ');

Map

MapIt is a structure of a group of key-value pairs, with extremely fast search speed.

For example, suppose you want to find the corresponding scores based on the students' names. If you use Array to implement them, two Arrays are needed:

var names = ['Michael', 'Bob', 'Tracy'];
var scores = [95, 75, 85];

Given a name, to find the corresponding score, you must first find the corresponding position in the names, and then take out the corresponding score from the scores. The longer the Array, the longer the time it takes.

If you use Map to implement it, you only need a comparison table of "name"-"score" and directly search for the scores based on the name. No matter how big this table is, the search speed will not slow down. Write a map in JavaScript as follows:

var m = new Map([['Michael', 95], ['Bob', 75], ['Tracy', 85]]);
('Michael'); // 95

initializationMapRequires a two-dimensional array, or directly initializes an empty oneMapMapHave the following methods:

var m = new Map(); // Empty Map('Adam', 67); // Add a new key-value('Bob', 59);
('Adam'); // Is there a key 'Adam': true('Adam'); // 67
('Adam'); // Delete key 'Adam'('Adam'); // undefined

Since a key can only correspond to one value, if you put a value on a key multiple times, the subsequent value will flush out the previous value:

var m = new Map();
('Adam', 67);
('Adam', 88);
('Adam'); // 88

Set

SetandMapSimilarly, it is also a collection of keys, but does not store value. Since the key cannot be repeated,SetNo duplicatekey
To create aSet, need to provide aArrayAs input, or create an emptySet

var s1 = new Set(); // Empty Setvar s2 = new Set([1, 2, 3]); // Includes 1, 2, 3

Repeat elements inSetAutomatically filtered:

var s = new Set([1, 2, 3, 3, '3']);
s; // Set {1, 2, 3, "3"}

Pay attention to the numbers3and strings'3'It's different elements.

passadd(key)Methods can add elements toSet, you can add it repeatedly, but it will not have any effect:

>>> (4)
>>> s
{1, 2, 3, 4}
>>> (4)
>>> s
{1, 2, 3, 4}

The delete(key) method can be used to delete elements:

var s = new Set([1, 2, 3]);
s; // Set {1, 2, 3}
(3);
s; // Set {1, 2}

summary

Map and Set are new data types added to the ES6 standard. Please decide whether to use them based on the browser's support.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.