Location>code7788 >text

JavaScript new map and new set use in detail (the difference between new map and new set)

Popularity:299 ℃/2024-07-24 12:11:39

Synopsis:
new Map(): In JavaScript, new Map() is used to create a new Map object, which is a collection of key-value pairs where keys are unique and values can be repeated.
new Set(): In JavaScript, new Set() is the syntax used to create a new Set object, which is a collection of unique values with no duplicates. new Set() can be used to create an empty Set object by passing in an array or array-like object at creation time, and Set will automatically remove duplicate values.
new Map()
I. new Map() Basic Features
new Map() is the constructor used to create a new Map object.The Map object holds the key-value pairs and remembers the original insertion order of the keys. This means that you can iterate over the Map object and get the key-value pairs in the order in which the keys were inserted.

Map objects are different from normal objects (which use strings as keys) because a Map can use any type as a key (including functions, objects, or any raw value), not just strings or symbols.

II. Relevant methodology
1. set(key, value) : Adds a key-value pair to the Map object.
2. get(key) : Get the value corresponding to the specified key.
3. has(key) : Determines whether the specified key exists in the Map object.
4. delete(key) : Deletes the specified key and its corresponding value.
5. size : Returns the number of key-value pairs in the Map object.
6. clear() : Clear all key-value pairs in the Map object.
7. keys() : Returns an iterator of all the keys in the Map object.
8. values() : Returns an iterator containing all the values in the Map object.
9. entries() : Returns an iterator containing all key-value pairs in the Map object.
III. Basic use

// Create a new Map object
let myMap = new Map();  
  
// 1. Add key-value pairs
('name', 'Alice');  
('age', 25);  
  
// 2、Get the value
(('name')); // Output: "Alice"
  
// 3. Check if the key exists
(('age')); // Output: true
  
// 4. Delete key-value pairs
myMap.delete('name');  
(('name')); // Output: false
 
// 5. Get the size of the Map
(); // Output: 1
  
// 6. Empty Map
();  
(); // Output: 0
  
// 7. Iterate over the keys
for (let key of ()) {  
  (key); // Output: "age"
}  
  
// 8, traversing the value
for (let value of ()) {  
  (value); // Output: 25
}  
  
// 9, traversing key-value pairs
for (let [key, value] of ()) {  
  (`Key: ${key}, Value: ${value}`); // Output: Key: age, Value: 25
}  

 


new Set()
I. new Set() Basic Features
Uniqueness: In Set, each value appears only once, allowing for simple array de-duplication; even if two objects are identical, they will only be stored once in Set.
Unordered: The elements of a Set are in no particular order.
II. Relevant methodology
add(value): Adds a value to the Set object. If the value already exists, nothing is done.
delete(value): Delete a value from the Set object. If the value exists, it is deleted and true is returned; otherwise, false is returned.
has(value): Returns a boolean indicating whether the Set object contains the specified value.
clear(): Clear the Set object, removing all elements.
III. Basic use

let setData = new Set();  
  
// Adding Elements
setData .add(1);  
setData .add(2);  
setData .add(3); 
(setData); // setData { 1, 2 ,3 }  
 
// Delete element
setData.delete(2);  
(setData); // Set { 1, 3 }  
 
  
// Checking for the existence of an element
((1)); // true  
((4)); // false  
  
// Iterate over elements
(value => (value));  
// Output.
// 1  
// 3
 
// Empty the collection
();  
();    // Output: 0, because the set is empty
 
 
//Initialize a Set by passing in an iterable object (such as an array) directly when creating the Set:
let set = new Set([1, 2, 2, 3, 4, 4]);  
//Simple array de-duplication
(set); // Set { 1, 2, 3, 4 }