Hash Maps in JavaScript


Hash Tables, better known as Hash Maps, are just collections of key-value pairs. In other words, they are pieces of data (values) mapped to unique identifiers called properties (keys).

Hash Maps were designed to give us a way of, given a key, associating a value with it for very quick lookups.

In JavaScript, an Object is implemented as a Hash Map behind the scenes. This is why adding and lookup of an Object's properties is so fast.

If you've worked with Objects in the past, you were working with Hash Maps without knowing it.


Creation:

You create an empty Hash Map just by creating an empty Object, like this:

let hashMap = {};

Creating an empty Hash Map takes O(1) time.


Initialization:

You can also add some key-value pairs in your Hash Map at creation:

let person = {
	'name': 'Andy',
	'age': 30,
	'hair color': 'brown'
};

Accessing values:

A value is retrieved from a Hash Map by specifying its corresponding key in square brackets ([]):

let person = {
	'name': 'Andy',
	'age': 30,
	'hair color': 'brown'
};

// Accessing values:
console.log(person['name']); // "Andy"
console.log(person['age']); // 30

// Accessing non-existing key:
console.log(person['job']); // undefined

As you can see, if you refer to a key that is not in the Hash Map, JavaScript returns undefined.

Accessing a value from a Hash Map takes O(1) time.


Adding an entry:

Adding an entry to an existing Hash Map is simply a matter of assigning a new key and value, via the assignment operator:

let person = {
	'name': 'Andy',
	'age': 30,
	'hair color': 'brown'
};

// Adding new entries:
person['job'] = 'Teacher';

let str = 'hobby';
person[str] = 'Fishing';

console.log(person);
/* The new Hash Map:
{
	'name': 'Andy',
	'age': 30,
	'hair color': 'brown',
	'job': 'Teacher',
	'hobby': 'Fishing'
	
} */

Adding a new entry to a Hash Map takes O(1) time.


Updating an entry:

If you want to update an entry, you can just assign a new value to an existing key:

let person = {
	'name': 'Andy',
	'age': 30,
	'hair color': 'brown'
};

// Updating entries:
person['name'] = 'Andrew';
person['age'] = 29;

console.log(person);
/* The new Hash Map:
{
	'name': 'Andrew',
	'age': 29,
	'hair color': 'brown'
} */

Updating an entry to a Hash Map takes O(1) time.


Deleting entries:

The delete operator allows you to remove a key from a Hash Map:

let person = {
	'name': 'Andy',
	'age': 30,
	'hair color': 'brown',
	'hobby': 'Fishing'
};

// Deleting entries:
delete person['name'];

let key = 'age';
delete person[key];

// Deleting a non-existent entry:
delete person['job']; // does nothing

console.log(person);
/* The new Hash Map:
{
	'hair color': 'brown',
	'hobby': 'Fishing'
} */

The Hash Map first checks if that key exists and if not, it does nothing and also doesn't raise an error, so it's safe to delete non-existing keys.

Deleting an entry from a Hash Map takes O(1) time.


Checking if a key exists:

You can check if a key exists in a Hash Map using the in operator:

let person = {
	'name': 'Andy',
	'age': 30,
	'hair color': 'brown',
	'hobby': 'Fishing'
};

let key = 'age';
console.log(key in person); // prints "true"

console.log('job' in person); // prints "false"

Checking if a key exists in a Hash Map takes O(1) time.


Iterating over the map keys:

If we want to iterate over keys of the dictionary, we can use the for loop along with the in operator:

let person = {
	'name': 'Andy',
	'age': 30,
	'hair color': 'brown',
	'hobby': 'Fishing'
};

for (let key in person) {
    console.log(key + " is " + person[key]);
}

// This will print the following:
// name is Andy
// age is 30
// hair color is brown
// hobby is Fishing


Iterating over a dictionary takes O(n) time.


Iterating over the dictionary values:

If we want to iterate over values of the map, we can use the for loop along with the of operator and the Object.values(map) function:

let person = {
	'name': 'Andy',
	'age': 30,
	'hair color': 'brown',
	'hobby': 'Fishing'
};

for (let val of Object.values(person) {
    console.log(val);
}

// This will print the following:
// Andy
// 30
// brown
// Fishing

Iterating over a dictionary takes O(n) time.


Iterating using both the key and the value for simplicity:

If we want to iterate over both keys and values of the map, we can use the for loop along with the of operator and the Object.entries(map) method:

let person = {
	'name': 'Andy',
	'age': 30,
	'hair color': 'brown',
	'hobby': 'Fishing'
}

for (let [key, val] of Object.entries(person)) {
    console.log(key + ' is ' +  val);
}

// This will print the following:
// name is Andy
// age is 30
// hair color is brown
// hobby is Fishing


Notice the order is not the same as initiated. Dictionary keeps the data in random order

Iterating over a dictionary takes O(n) time.


Assignment
Follow the Coding Tutorial and let's play with some Hash Maps.


Hint
Look at the examples above if you get stuck.


Introduction

In this lesson, we will explore the concept of Hash Maps in JavaScript. Hash Maps, also known as Hash Tables, are a fundamental data structure in programming. They allow us to store and retrieve data efficiently using key-value pairs. Understanding Hash Maps is crucial for solving various programming problems, especially those involving quick lookups and data organization.

Hash Maps are widely used in scenarios where fast data retrieval is essential, such as caching, database indexing, and implementing associative arrays. By mastering Hash Maps, you can significantly improve the performance of your applications.

Understanding the Basics

Before diving into the details, let's understand the fundamental concepts of Hash Maps:

Understanding these basics is essential before moving on to more complex aspects of Hash Maps.

Main Concepts

Let's delve into the key concepts and techniques involved in Hash Maps:

Examples and Use Cases

Let's explore some examples and real-world use cases of Hash Maps:

Common Pitfalls and Best Practices

When working with Hash Maps, it's important to be aware of common pitfalls and follow best practices:

Advanced Techniques

Once you are comfortable with the basics, you can explore advanced techniques related to Hash Maps:

Code Implementation

Here are some well-commented code snippets demonstrating the correct use of Hash Maps:

// Creating an empty Hash Map
let hashMap = {};

// Initializing a Hash Map with key-value pairs
let person = {
  'name': '