Working with Sets and Maps in ES6

Sets in ES6

A set is an unordered collection of unique values in JavaScript. It allows us to store any type of value like strings, numbers, objects, etc., while ensuring uniqueness. Let’s see how to create and work with sets in ES6.

Creating a Set

To create a set, we can simply use the Set constructor:

const mySet = new Set();

We can also initialize a set with values using an array:

const mySet = new Set([1, 2, 3]);

Adding and Deleting Elements

To add elements to a set, we can use the add() method:

mySet.add(4);
mySet.add("hello");

To delete elements from a set, we can use the delete() method:

mySet.delete(3);

Checking the Size and Clearing a Set

We can get the size of a set using the size property:

console.log(mySet.size);

To clear all the elements in a set, we can use the clear() method:

mySet.clear();

Checking if an Element Exists in a Set

We can check if an element exists in a set using the has() method:

console.log(mySet.has("hello")); // true
console.log(mySet.has("world")); // false

Iterating Over a Set

To iterate over the elements of a set, we can use the for...of loop:

for (let element of mySet) {
  console.log(element);
}

Maps in ES6

A map is a collection of key-value pairs in JavaScript. It allows us to store any type of key and value, and provides efficient methods to retrieve, add, and delete entries. Let’s see how to create and work with maps in ES6.

Creating a Map

To create a map, we can use the Map constructor:

const myMap = new Map();

We can also initialize a map with key-value pairs using an array of arrays:

const myMap = new Map([
  ["name", "John"],
  ["age", 25],
]);

Adding and Deleting Entries

To add entries to a map, we can use the set() method:

myMap.set("email", "john@example.com");
myMap.set("address", "123 Street");

To delete entries from a map, we can use the delete() method:

myMap.delete("age");

Checking the Size and Clearing a Map

We can get the size of a map using the size property:

console.log(myMap.size);

To clear all the entries in a map, we can use the clear() method:

myMap.clear();

Checking if a Key Exists in a Map

We can check if a key exists in a map using the has() method:

console.log(myMap.has("name")); // true
console.log(myMap.has("address")); // false

Retrieving Values from a Map

To retrieve a value from a map, we can use the get() method:

console.log(myMap.get("name")); // John

Iterating Over a Map

To iterate over the entries of a map, we can use the for...of loop:

for (let [key, value] of myMap) {
  console.log(key, value);
}

Conclusion

Sets and maps are powerful data structures in ES6 that provide efficient ways to work with unique values and key-value pairs. They can be used in various scenarios to simplify and optimize our JavaScript code. By incorporating sets and maps into our applications, we can enhance performance and improve readability. So, start exploring these data structures and leverage their benefits in your JavaScript projects. #ES6 #JavaScriptDataStructures