30 Days of JAVASCRIPT #8/30
3.Sets and Maps data structures
Maps and sets are two important data structures in JavaScript that were introduced in ECMAScript 2015.
In this blog post, we will cover the following topics:
What is a Set?
How to create a Set
Properties and methods of a Set
What is a Map?
How to create a Map
Properties and methods of a Map
Example code
What is a Set?
A set is a collection of unique values of any type. Sets are iterable, which means we can loop through them using afor...of
loop or theforEach()
method. Sets are useful when we want to store a collection of values without duplicates.
How to create a Set
To create a set, we use thenew
keyword followed by theSet()
constructor. For example, we can create a set of numbers as follows:
const numbers = new Set([1, 2, 3, 4, 5]);
Properties and methods of a Set
Sets have several properties and methods that we can use to manipulate them. Here are some of the most commonly used ones:
add(value)
: adds a new value to the sethas(value)
: returns a boolean indicating whether the set contains the specified valuedelete(value)
: removes the specified value from the setclear()
: removes all values from the setsize
: returns the number of values in the set
What is a Map?
A map is a collection of key-value pairs where the keys can be of any type. Maps are iterable, which means we can loop through them using afor...of
loop or theforEach()
method. Maps are useful when we want to store a collection of values that are associated with specific keys.
How to create a Map
To create a map, we use thenew
keyword followed by theMap()
constructor. For example, we can create a map of people's ages as follows:
const ages = new Map([
["John", 30],
["Jane", 25],
["Bob", 40]
]);
Properties and methods of a Map
Maps have several properties and methods that we can use to manipulate them. Here are some of the most commonly used ones:
set(key, value)
: sets the value of the specified keyget(key)
: returns the value associated with the specified keyhas(key)
: returns a boolean indicating whether the map contains the specified keydelete(key)
: removes the specified key-value pair from the mapclear()
: removes all key-value pairs from the mapsize
: returns the number of key-value pairs in the map
Example code
Here's an example code that demonstrates the creation, manipulation, and iteration of sets and maps:
// Create a set of numbers
const numbers = new Set([1, 2, 3, 4, 5]);
// Add a new number to the set
numbers.add(6);
// Check if the set contains a specific number
console.log(numbers.has(3)); // Output: true
// Remove a number from the set
numbers.delete(4);
// Loop through the set using forEach()
numbers.forEach(number => {
console.log(number);
});
// Create a map of people's ages
const ages = new Map([
["John", 30],
["Jane", 25],
["Bob", 40]
]);
// Set the age of a specific person
ages.set("Mary", 35);
// Get the age of a specific person
console.log(ages.get("John")); // Output: 30
// Check if the map contains a specific person
console.log(ages.has("Bob")); // Output: true
// Remove a person from the map
ages.delete("Jane");
// Loop through the map using for...of
for (const [person, age] of ages) {
console.log(`${person} is ${age} years old`);
}
In conclusion, sets and maps are powerful data structures in JavaScript that allow us to store and manipulate collections of values in a simple and organized way. They have many useful properties and methods that make them versatile and easy to use.