Start learning free

LESSON 355 OF 675 · HASH TABLE

Hash Maps in TypeScript

Lesson

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 TypeScript, as 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, the result is 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 Hash Map, 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 Hash Map takes O(n) time.


Iterating over the Hash Map values:

If we want to iterate over values of the Hash Map, we can use the for loop along with the of operator and the Object.values(obj) 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 Hash Map 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 Hash Map, we can use the for loop along with the of operator and the Object.entries(obj) 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: the keys come out in the order they were added (except that integer-like keys such as '1' come first, in ascending order).

Iterating over a Hash Map 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.


STEP 1 OF 7
    Ctrl + Enter to check