- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Creating a hash table using Javascript
Let us set up a simple class that we'll use to define all these methods on. We'll create a container object to store the hash table and create a display function to display the table. Note that for collision resolution, we'll use chaining.
The display function takes each entry (hashed value) in the table and prints all pairs associated with it.
Example
We'll also create a new class on the prototype to store the key-value pairs.
class HashTable { constructor() { this.container = []; // Populate the container with empty arrays // which can be used to add more elements in // cases of collisions for(let i=0; i < 11; i ++ ) { this.container.push([]); } display() { this.container.forEach((value, index) => { let chain = value .map(({ key, value }) => `{ ${key}: ${value} }`) .join(" --> "); console.log(`${index}: ${chain}`); }); } hash(key) { return key % 11; } } HashTable.prototype.KVPair = class { constructor(key, value) { this.key = key; this.value = value; } } }
We're using some advanced features like destructuring in the display method. That helps avoid boilerplate code.
- Related Articles
- Loop through a hash table using Javascript
- Add elements to a hash table using Javascript
- Hash Table Data Structure in Javascript
- Remove elements from Javascript Hash Table
- Creating a MySQL table using Node.js
- Search Element in an Javascript Hash Table
- Creating a Set using Javascript
- Creating a BinaryTree using Javascript
- Creating a table look-a-like using Tkinter
- Creating a MySQL Table in NodeJS using Sequelize
- Creating a Priority Queue using Javascript
- Creating a linked list using Javascript
- Creating Arrays using Javascript
- Creating Dictionary using Javascript
- Creating a table using SAP HANA Studio UI option

Advertisements