Two Important Method  In Javascript|| Set() && Map() ||

Two Important Method In Javascript|| Set() && Map() ||

Javascript

Basic Set Functionality

A set is an object in JavaScript that allows you to store unique values of any type. It is similar to an array, but unlike an array, a set does not allow duplicate values. Here is an example of how to create a set:

javascriptCopy codeconst mySet = new Set();

This creates an empty set. To add an element to the set, you can use the add method:

javascriptCopy codemySet.add(1);
mySet.add(2);
mySet.add(3);

This adds the elements 1, 2, and 3 to the set. Note that if you try to add an element that already exists in the set, it will be ignored.

You can also initialize a set with an array of values:

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

To get the number of elements in the set, you can use the size property:

javascriptCopy codeconsole.log(mySet.size); // Output: 3

To check if a value is in the set, you can use the has method:

javascriptCopy codeconsole.log(mySet.has(2)); // Output: true
console.log(mySet.has(4)); // Output: false

To delete an element from the set, you can use the delete method:

javascriptCopy codemySet.delete(2);
console.log(mySet.has(2)); // Output: false

You can also clear the entire set using the clear method:

javascriptCopy codemySet.clear();
console.log(mySet.size); // Output: 0

Advanced Set Functionality

Iterating Over a Set

You can use the for...of the loop to iterate over the elements of a set:

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

for (const element of mySet) {
  console.log(element);
}
// Output:
// 1
// 2
// 3

Set Operations

Sets support various operations such as union, intersection, and difference. Here are some examples:

javascriptCopy codeconst setA = new Set([1, 2, 3]);
const setB = new Set([2, 3, 4]);

// Union
const union = new Set([...setA, ...setB]);
console.log(union); // Output: Set(4) {1, 2, 3, 4}

// Intersection
const intersection = new Set([...setA].filter(x => setB.has(x)));
console.log(intersection); // Output: Set(2) {2, 3}

// Difference
const difference = new Set([...setA].filter(x => !setB.has(x)));
console.log(difference); // Output: Set(1) {1}

Converting a Set to an Array

You can convert a set to an array using the spread operator:

javascriptCopy codeconst mySet = new Set([1, 2, 3]);
const myArray = [...mySet];
console.log(myArray); // Output: [1, 2, 3]

Weak Sets

A weak set is a special type of set where the elements are weakly held. This means that if an element in the set is not referenced anywhere else, it will be garbage collected. Weak sets are useful when you want to associate additional data with an object without affecting its memory management. Here is an example:

javascriptCopy codeconst myWeakSet = new Weak

Map Functionality

The Map() object is a collection of key-value pairs, where each key is unique. It provides a way to store and retrieve data based on a key, similar to an object, but with some differences in functionality.

  1. Basic Syntax: The syntax for creating a new Map() object is as follows:
javascriptCopy codelet myMap = new Map();
  1. Adding Elements: To add a new key-value pair to a Map() object, you can use the set() method as follows:
javascriptCopy codemyMap.set(key, value);

Here, key can be of any data type (string, number, object, etc.), and value can also be of any data type (string, number, object, etc.). For example:

javascriptCopy codelet myMap = new Map();
myMap.set(1, "one");
myMap.set(2, "two");
myMap.set(3, "three");

This creates a new Map() object myMap with three key-value pairs.

  1. Getting Elements: To retrieve the value associated with a particular key in a Map() object, you can use the get() method as follows:
javascriptCopy codelet value = myMap.get(key);

Here, key is the key for which you want to retrieve the value. For example:

javascriptCopy codelet myMap = new Map();
myMap.set(1, "one");
myMap.set(2, "two");
myMap.set(3, "three");

let value = myMap.get(2);
console.log(value); // Output: "two"

This retrieves the value associated with the key 2 in the Map() object.

  1. Checking for Existence: To check if a key exists in a Map() object, you can use the has() method as follows:
javascriptCopy codelet hasKey = myMap.has(key);

Here, key is the key you want to check for existence. For example:

javascriptCopy codelet myMap = new Map();
myMap.set(1, "one");
myMap.set(2, "two");
myMap.set(3, "three");

let hasKey = myMap.has(2);
console.log(hasKey); // Output: true

This checks if the key 2 exists in the Map() object.

  1. Removing Elements: To remove a key-value pair from a Map() object, you can use the delete() method as follows:
javascriptCopy codemyMap.delete(key);

Here, key is the key you want to remove. For example:

javascriptCopy codelet myMap = new Map();
myMap.set(1, "one");
myMap.set(2, "two");
myMap.set(3, "three");

myMap.delete(2);
console.log(myMap); // Output: Map { 1 => 'one', 3 => 'three' }

This removes the key-value pair with the key 2 from the Map() object.

  1. Iterating through Map(): You can use the forEach() method to iterate through all the key-value pairs in a Map() object as follows:
javascriptCopy codemyMap.forEach(function(value, key) {
    console.log(key + " = " + value);
});

Here, the function is called for each key-value pair in the Map() object, and key and value are the key-value pairs. For example:

javascriptCopy codelet myMap = new Map();
myMap.set(1, "one");
myMap.set(2, "two");
myMap.set(3, "three");

Did you find this article valuable?

Support shiv singh baghel by becoming a sponsor. Any amount is appreciated!