# JS Data Structure Notes

## Published on 01/24/2019

### 2 min read

## In category

### JavaScript

# What is a Data Structure?

#### Data structures are collections of values, the relationships among them, and the functions or operations that can be applied to the data.

# 1. Hash Table (a.k.a. Hash Map)

- used to store key-value pairs
- keys in a hash-table are not ordered
- very fast CRUD functionality.
- great for
**non-sequential data**

#### Javascript's Object and Map are both hashes.

## Color Example

```
const colors = ['#ff343', '#ff939', '#ff920'];
// before hashing
colors[2];
// after hashing
colors['cyan'];
```

#### The 'colors' listed are not very 'human readable'. Hash table to the rescue!

#### Basic gist of a hash function is such that every time a unique input gets passed in, only one unique output is returned.

```
pink => 1
1 => pink
```

#### to implement a rudimentary hash function we can use the `charCodeAt()`

function

```
"a".charCodeAt(0) // 97, 97 is the char code at the 0th (first) value of the string on which the charCodeAt() function was called.
"hi".charCodeAt(0) // 104
"hi".charCodeAt(1) // 105
// NEAT TRICK if you subtract 96, this gives us the 'alphabetic ranking', aka, "a" is the 1st letter in the alphabet.
"a".charCodeAt(0) - 96 // 1
```

#### We can leverage the modulo operator to constrain the hash output beneath a chosen value.

```
52 % 11 // 8
2351431234 % 11 // 9
```

#### whatever number we modulo by, we are guaranteed to get an output that is smaller than that number, no matter how large the first number is.

# What is a Graph?

#### A **graph** is a collection of nodes and connections between those nodes.

- there are no entry points, or start/finish. The nodes are treated equally, and the connections between them are part of the definition of the graph
- great fro recommendations and overlap analysis.
- i.e. two video games are linked by "war", "space", and "future", nodes.