Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Javascript Articles
Page 398 of 534
The Set Class in Javascript
JavaScript's built-in Set class is a collection of unique values that allows you to store distinct elements of any type. Unlike arrays, Sets automatically prevent duplicate values and provide efficient methods for adding, removing, and checking element existence. Creating a Set You can create a Set using the new Set() constructor, optionally passing an iterable like an array: // Empty set let mySet = new Set(); // Set from array let numbersSet = new Set([1, 2, 3, 4, 4, 5]); console.log(numbersSet); // Duplicates are automatically removed let wordsSet = new Set(['hello', 'world', 'hello']); console.log(wordsSet); ...
Read MoreDictionary Data Structure in Javascript
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 ...
Read MoreCreating Dictionary using Javascript
In JavaScript, dictionaries can be created using objects, the ES6 Map class, or custom implementations. Each approach offers different features and use cases. Using Objects as Dictionaries The simplest way to create a dictionary is using plain JavaScript objects: // Creating a dictionary using object literal const dict = { "key1": "value1", "key2": "value2", "key3": "value3" }; console.log(dict["key1"]); console.log(dict.key2); value1 value2 Using ES6 Map Class The Map class provides a more robust dictionary implementation with additional methods: ...
Read MoreRemove elements from a Dictionary using Javascript
In JavaScript, there are multiple ways to remove elements from dictionary-like structures. This depends on whether you're working with plain objects, custom dictionary classes, or ES6 Maps. Using delete Operator with Objects For plain JavaScript objects acting as dictionaries, use the delete operator: const dict = { key1: "value1", key2: "value2", key3: "value3" }; console.log("Before deletion:", dict); delete dict.key2; console.log("After deletion:", dict); console.log("key2 exists:", "key2" in dict); Before deletion: { key1: 'value1', key2: 'value2', key3: 'value3' } After ...
Read MoreSearch Element in a Dictionary using Javascript
In JavaScript, searching for elements in a dictionary (key-value pairs) can be accomplished using custom dictionary implementations or built-in ES6 Map objects. Both approaches provide efficient key-based lookups. Custom Dictionary Implementation Here's how to implement a get method that searches for a given key in a custom dictionary: get(key) { if(this.hasKey(key)) { return this.container[key]; } return undefined; } JavaScript objects are implemented like dictionaries internally, providing optimized key-value operations. This makes direct property access ...
Read MoreThe Keys and values method in Javascript
JavaScript provides several methods to extract keys and values from objects and Maps. The most common approaches are Object.keys(), Object.values() for plain objects, and the keys(), values() methods for ES6 Maps. Getting Keys from Objects Use Object.keys() to extract all property names from an object as an array: const myObject = { key1: "value1", key2: "value2", key3: "value3" }; const keys = Object.keys(myObject); console.log(keys); [ 'key1', 'key2', 'key3' ] Getting Values from Objects Object.values() extracts all property ...
Read MoreLoop through a Dictionary in Javascript
In JavaScript, a dictionary (or object) stores key-value pairs. There are several ways to loop through these pairs depending on whether you're working with plain objects, ES6 Maps, or custom implementations. Method 1: Using for...in Loop (Plain Objects) The for...in loop is the traditional way to iterate through object properties: const dictionary = { key1: "value1", key2: "value2", key3: "value3" }; for (let key in dictionary) { console.log(`Key: ${key}, Value: ${dictionary[key]}`); } Key: key1, Value: value1 ...
Read MoreThe Dictionary Class in Javascript
JavaScript doesn't have a built-in Dictionary class, but we can create our own using objects or the Map class. Here's a complete implementation of a custom Dictionary class called MyMap. Complete Dictionary Implementation class MyMap { constructor() { this.container = {}; } display() { console.log(this.container); } hasKey(key) { return key in this.container; } ...
Read MoreCreating a hash table using Javascript
A hash table (also called hash map) is a data structure that stores key-value pairs and provides fast lookup, insertion, and deletion operations. In JavaScript, we can implement a hash table using arrays and a hash function to map keys to array indices. Basic Hash Table Structure Let's create a hash table class with collision resolution using chaining. We'll use an array of arrays where each index can hold multiple key-value pairs in case of hash collisions. class HashTable { constructor() { this.container = ...
Read MoreAdd elements to a hash table using Javascript
When adding elements to a hash table, the most crucial part is collision resolution. We're going to use chaining for the same. There are other algorithms you can read about here: https://en.wikipedia.org/wiki/Hash_table#Collision_resolution Now let's look at the implementation. We'll be creating a hash function that'll work on integers only to keep this simple. But a more complex algorithm can be used to hash every object. Hash Table Implementation First, let's create a complete hash table class with the necessary components: class HashTable { constructor(size = 11) { ...
Read More