Skip to content

Instantly share code, notes, and snippets.

@Shurlow
Last active December 13, 2018 17:30
Show Gist options
  • Save Shurlow/aaee715f1a4246bfc9ea4a9cbb15b815 to your computer and use it in GitHub Desktop.
Save Shurlow/aaee715f1a4246bfc9ea4a9cbb15b815 to your computer and use it in GitHub Desktop.
Lesson on creating custom data structures

Custom Data Structures

Objectives

  • Explain what a data structure is
  • Use JavaScripts Map data structure
  • Use JavaScripts Set data structure
  • Build a custom data structure

Explain what a data structure is

  • Turn to your neighbor and discuss what a data structure is in your own words.

    Your answer...

  • What are some examples of data structures we've used in JavaScript?

    Your answer...

Map

Take a look at the new Map data structure

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

const obj = {}

obj.name = 'Rocko' // set key & value
obj.name // returns value 'Rocko'
const map = new Map()

map.set('name', 'Rocko') // set key & value
map.get('name') // returns value 'Rocko'

map.size // counts key-value pairs

Example

https://repl.it/@galvanize/MapExample

Set

Take a look at the new Set data structure

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set

How does a set differ from a Map?

const s = new Set()

s.add('Rocko') // adds item to set
s.add('Spunky')
s.add('Rocko') // does NOT add duplicate item

s.size() // counts items in set

Example

https://repl.it/@scottyhurlow/SetExample

Creating custom data structures

Take a look at the following repo: CustomSet.

Fork & clone the repo. Follow the tests to implement the CustomSet data structure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment