- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Joining two hash tables in Javascript
Sometimes we need to combine containers together using a join function and get a new container. We'll write a static join method that takes in 2 HashTables and creates a new HashTable with all the values. For the sake of simplicity, we'll let the values from the second one override the values for the first one if there are any keys present in both of them.
Example
static join(table1, table2) { // Check if both args are HashTables if(!table1 instanceof HashTable || !table2 instanceof HashTable) { throw new Error("Illegal Arguments") } let combo = new HashTable(); table1.forEach((k, v) => combo.put(k, v)); table2.forEach((k, v) => combo.put(k, v)); return combo; }
You can test this using −
Example
let ht1 = new HashTable(); ht1.put(10, 94); ht1.put(20, 72); ht1.put(30, 1); let ht2 = new HashTable(); ht2.put(21, 6); ht2.put(15, 21); ht2.put(32, 34); let htCombo = HashTable.join(ht1, ht2) htCombo.display();
Example
This will give the output −
0: 1: 2: 3: 4: { 15: 21 } 5: 6: 7: 8: { 30: 1 } 9: { 20: 72 } 10: { 10: 94 } --> { 21: 6 } --> { 32: 34 }
- Related Articles
- Hash Functions and Hash Tables
- How to add/merge two hash tables in PowerShell?
- Distributed Hash Tables (DHTs)
- Joining two Arrays in Javascript
- C++ Program to Implement Hash Tables
- Hash Tables for Integer Keys in Data Structure
- Construct objects from joining two strings JavaScript
- What is Search Tree and Hash Tables in compiler design?
- Joining two strings with two words at a time - JavaScript
- C++ Program to Implement Hash Tables with Double Hashing
- C++ Program to Implement Hash Tables with Linear Probing
- C++ Program to Implement Hash Tables with Quadratic Probing
- C++ Program to Implement Hash Tables Chaining with List Heads
- How to create and populate Java array of Hash tables?
- C++ Program to Implement Hash Tables Chaining with Doubly Linked Lists

Advertisements