Implement a multikey map in JavaScript
This post will discuss how to implement a multikey map in JavaScript.
A multikey map is a data structure that allows us to associate more than one key with a single value. For example, we could have a multikey map that stores the names of countries and their capitals, such that we can access the capital by using either the country name or the country code as a key. There are several ways to implement a multikey map in JavaScript:
1. Using a regular map
One way to implement a multikey map is to use an array of keys and a regular map object. The array of keys contains all the possible keys that can be used to access the value, and the map object maps each key to the same value. This way, we can access the value using any of the keys. Here’s an example of this approach:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
// Create an array of keys let keys = ["United States of America", "United States", "USA", "US"]; // Create a value let value = "Washington, D.C."; // Create a regular map object let map = new Map(); // Loop through the keys and map each one to the value for (let key of keys) { map.set(key, value); } // Access the value using any of the keys console.log(map.get("United States of America")); // "Washington, D.C." console.log(map.get("United States")); // "Washington, D.C." console.log(map.get("USA")); // "Washington, D.C." console.log(map.get("US")); // "Washington, D.C." console.log(map); /* Map(4) { 'United States of America' => 'Washington, D.C.', 'United States' => 'Washington, D.C.', 'USA' => 'Washington, D.C.', 'US' => 'Washington, D.C.' } */ |
2. Using a custom class
Another way to implement a multikey map is to use a custom class that extends the built-in Map class and overrides some of its functions. The custom class can store an array of keys and a value for each entry, and provide functions to set, get, and delete values using any of the keys. Here’s an example of how we can achieve this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
// Define a custom class that extends Map class MultiKeyMap extends Map { // Constructor that takes an iterable of entries constructor(iterable) { // Call the super constructor super(); // Loop through the entries and add them to the map for (let [keys, value] of iterable) { this.set(keys, value); } } // Override the get function to accept any key and return the value get(key) { // Return the value associated with the key return super.get(key); } // Override the set function to accept an array of keys and a value set(keys, value) { // Loop through the keys and set each one to the value for (let key of keys) { super.set(key, value); } // Return this for chaining return this; } // Override the delete function to accept any key and delete the entry delete(key) { // Get the value associated with the key let value = this.get(key); // Loop through the entries and delete any that have the same value for (let [k, v] of this) { if (v === value) { super.delete(k); } } // Return true if an entry was deleted, false otherwise return !!value; } } // Create a custom multikey map with some entries let map = new MultiKeyMap([ [["United States of America", "United States", "USA", "US"], "Washington, D.C."], [["India", "Republic of India", "Bharat"], "New Delhi"], [["United Kingdom", "Britain", "UK"], "London"] ]); // Access the values using any of the keys console.log(map.get("United States")); // "Washington, D.C." console.log(map.get("India")); // "New Delhi" console.log(map.get("UK")); // "London" map.delete("Britain"); console.log(map); /* MultiKeyMap(7) [Map] { 'United States of America' => 'Washington, D.C.', 'United States' => 'Washington, D.C.', 'USA' => 'Washington, D.C.', 'US' => 'Washington, D.C.', 'India' => 'New Delhi', 'Republic of India' => 'New Delhi', 'Bharat' => 'New Delhi' } */ |
That’s all about implementing a multikey map in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)