Found 6710 Articles for Javascript

Add elements to a Dictionary in Javascript

Monica Mona
Updated on 15-Jun-2020 10:49:38

10K+ Views

Now we'll create the put method that'll allow us to put key-value pairs on the dictionary. Now using this we'll implement the put method.Note that JS has objects that act quite like dictionaries. We can just set the container's key property to value. Exampleput(key, value) {    this.container[key] = value; }You can test this and the previous functions using − Exampleconst myMap = new MyMap() myMap.put("key1", "value1") myMap.put("key2", "value2") myMap.display() console.log(myMap.hasKey("key1")); console.log(myMap.hasKey("key3"));OutputThis will give the output −{key1: "value1", key2: "value2"} true falseIn ES6, you can put a key-value pair in a map using the set method. For example,  Exampleconst myMap ... Read More

Creating Dictionary using Javascript

Monica Mona
Updated on 15-Jun-2020 10:52:03

2K+ Views

Let's create a MyMap class so that it doesn't hide the actual Map class in JS. We'll create a container object that'll keep track of all our values that we add to the map. We'll also create a display function that prints the map for us. Exampleclass MyMap {    constructor() {       this.container = {};    }    display() {       console.log(this.container);    } }In ES6, you can directly create a dictionary using the Map class. For example,  Exampleconst map1 = new Map(); const map2 = new Map([    ["key1", "value1"],    ["key2", "value2"] ]);Checking if ... Read More

Dictionary Data Structure in Javascript

Samual Sam
Updated on 15-Jun-2020 10:53:14

1K+ Views

In computer science, an associative array, map, symbol table, or dictionary is an abstract data type composed of a collection of (key, value) pairs, such that each possible key appears at most once in the collection. Note that a dictionary is also known as a map.The dictionary problem is a classic computer science problem: the task of designing a data structure that maintains a set of data during 'search', 'delete', and 'insert' operations. There are many different types of implementations of dictionaries. Hash Table implementation Tree-Based Implementation (Self-balancing and Unbalanced trees) List based implementationWhen to use a DictionaryDictionaries are not a silver bullet ... Read More

The Set Class in Javascript

Samual Sam
Updated on 15-Jun-2020 09:30:31

146 Views

Here is the complete implementation of the MySet class. Exampleclass MySet {    constructor() {       this.container = {};    }    display() {       console.log(this.container);    }    has(val) {       return this.container.hasOwnProperty(val);    }    add(val) {       if (!this.has(val)) {          this.container[val] = val;          return true;       }       return false;    }    delete(val) {       if (this.has(val)) {          delete this.container[val];          return true;       }   ... Read More

Subtract two Sets in Javascript

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

3K+ Views

The difference of 2 sets means the set being subtracted should have all its elements removed from the set it is being subtracted from. So we can iterate over the second set and remove all the elements present in it from the first set. Examplestatic difference(s1, s2) {    if (!s1 instanceof MySet || !s2 instanceof MySet) {       console.log("The given objects are not of type MySet");       return null;    }    let newSet = new MySet();    s1.forEach(elem => newSet.add(elem));    s2.forEach(elem => newSet.delete(elem));    return newSet; }You can test this using − Exampleconst testSet1 = ... Read More

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

Loop through a Set using Javascript

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

614 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.

Clearing the set using Javascript

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

160 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 } { }

Remove elements from a Set using Javascript

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

204 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

Add elements to a Set using Javascript

Vivek Verma
Updated on 24-Jul-2025 15:31:09

234 Views

In JavaScript, a Set is an object that is used to store a collection of unique elements of different types, such as numbers, strings, booleans, and object references, etc. const mySet = new Set(); Adding Elements to a JavaScript Set We can add elements to a JavaScript set in the following ways: Using Simple add() Method Using Method Chaining with add() Using add() Method The add() method of the Set object accepts a value as a parameter and adds it to the elements of the current Set object, and returns ... Read More

Advertisements