Loop through a Dictionary in Javascript

Monica Mona
Updated on 15-Jun-2020 10:34:46

12K+ Views

Here we'll implement a for each function in our class and accept a callback that we can call on every key-value pair. Let's see how we can implement such a function − ExampleforEach(callback) {    for (let prop in this.container) {       // Call the callback as: callback(key, value)       callback(prop, this.container[prop]);    } }You can test this using − Exampleconst myMap = new MyMap(); myMap.put("key1", "value1"); myMap.put("key2", "value2"); myMap.forEach((k, v) => console.log(`Key is ${k} and value is ${v}`));OutputThis will give the output −Key is key1 and value is value1 Key is key2 and value is value2ES6 ... Read More

The Dictionary Class in Javascript

Samual Sam
Updated on 15-Jun-2020 10:31:45

494 Views

Here is the complete implementation of MyMap class − Exampleclass MyMap {    constructor() {       this.container = {};    }    display() {       console.log(this.container);    }    hasKey(key) {       return key in this.container;    }    put(key, value) {       this.container[key] = value;    }    delete(key) {       if (this.hasKey(key)) {          delete this.container[key];          return true;       }       return false;    }    get(key) {       return this.hasKey(key) ? this.container[key] : undefined;   ... Read More

Hash Table Data Structure in Javascript

Samual Sam
Updated on 15-Jun-2020 10:31:03

2K+ Views

Hash Table is a data structure which stores data in an associative manner. In a hash table, data is stored in an array format, where each data value has its own unique index value. Access to data becomes very fast if we know the index of the desired data.Thus, it becomes a data structure in which insertion and search operations are very fast irrespective of the size of the data. Hash Table uses an array as a storage medium and uses the hash technique to generate an index where an element is to be inserted or is to be located ... Read More

Set Data Structure in Javascript

karthikeya Boyini
Updated on 15-Jun-2020 09:52:17

563 Views

A set is an abstract data type that can store certain values, without any particular order and no repeated values. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests a value for membership in a set.

When should you use sets in Javascript?

Samual Sam
Updated on 15-Jun-2020 09:50:56

217 Views

Whenever you want to store unique elements in a container for which the order doesn't matter and you mainly want to use it to check for membership of different objects.Sets are also useful when you want to perform operations like union, intersection, a difference like you do in mathematical sets.Let's look at both how we can define our own set and use the existing one in ES6.Methods we'll implementThe ES6 set API provides some methods. We'll implement these methods in our implementation and also look at how to use them using the built-in class.add() −  Adds a new element to the ... Read More

Creating a Set using Javascript

karthikeya Boyini
Updated on 15-Jun-2020 09:48:57

642 Views

Let's create a MySet class so that it doesn't hide the actual set class in JS. We'll create a container object that'll keep track of all our values that we add to the set. We'll also create a display function that prints the set for us. Exampleclass MySet {    constructor() {       this.container = {};    }    display() {       console.log(this.container);    } }In ES6, you can directly create a set using the Set class. For example,  Exampleconst set1 = new Set(); const set2 = new Set([1, 2, 5, 6]);Checking for membershipThe has method checks ... Read More

Remove elements from a Set using Javascript

karthikeya Boyini
Updated on 15-Jun-2020 09:42:29

246 Views

The delete method checks if a value already exists in the set, if it does, then it removes that value from the set. We can implement it as follows &minusl Exampledelete(val) {    if (this.has(val)) {       delete this.container[val];       return true;    }    return false; }You can test this using − Exampleconst testSet = new MySet(); testSet.add(1); testSet.add(2); testSet.add(5); testSet.delete(5); testSet.delete(2); testSet.display(); console.log(testSet.has(5)); console.log(testSet.has(20)); console.log(testSet.has(1));OutputThis will give the output −{ '1': 1} False False TrueIn ES6, you use the delete function as follows − Exampleconst testSet = new MySet(); testSet.add(1); testSet.add(2); testSet.add(5); testSet.delete(5); ... Read More

Clearing the set using Javascript

Samual Sam
Updated on 15-Jun-2020 09:39:55

183 Views

The clear method is pretty straightforward. We can just reassign the container variable to a new object and the set will now be empty. This can be implemented as follows − Exampleclear() {    this.container = {}; }You can test this using −Exampleconst testSet = new MySet(); testSet.add(1); testSet.add(2); testSet.add(5); testSet.display(); testSet.clear(); testSet.display();OutputThis will give the output −{ '1': 1, '2': 2, '5': 5 } { }

Loop through a Set using Javascript

karthikeya Boyini
Updated on 15-Jun-2020 09:37:52

691 Views

In the set that we implemented, we can create a for each function in our class and accept a callback that we can call on every element. Let's see how we can implement such a function − ExampleforEach(callback) {    for (let prop in this.container) {       callback(prop);    } }You can test this using − Exampleconst testSet = new MySet(); testSet.add(1); testSet.add(2); testSet.add(5); testSet.forEach(elem => console.log(`Element is ${elem}`));OutputThis will give the output −Element is 1 Element is 2 Element is 5The ES6 Set API also provides the same functionality using the forEach method.

Adding two Sets in Javascript

Samual Sam
Updated on 15-Jun-2020 09:36:21

3K+ Views

The operation of adding 2 sets is known as a union. You need to add every object from one set to another while checking for duplicates. We can just use the 2 methods we already implemented to implement this method.We'll implement this function as a static function as we don’t want to mutate existing sets, but create and return a new one. We first need to check if the object passed to it is really an instance of the MySet class. Examplestatic union(s1, s2) {    if (!s1 instanceof MySet || !s2 instanceof MySet) {       console.log("The given objects ... Read More

Advertisements