Data Structures — Set & Map
Set
A set is a dynamic data structure with NO duplicates. A set is unordered, and has no index. Sets can store any types of values (primitive or objects).
Methods for a Set: .has(), .add(), .delete(), .size(), .cleare()
Used for: easily removes all duplicates from an array
const myArray = ['Bill','Bob','Bob','Ben']
const mySet = new Set(myArray)
let uniqueArray = mySet // ['Bill','Bob',Ben']
Map Constructor
Key value pairs with distinct keys. Remembers the original insertion order of the keys
Methods for a Map: .has(),.set(),.delete(),.size(),.clear()
Why we use Map() instead of a Javascript Object? Objects only support one key object, but maps support multiple key objects
const a = {}
const b = {}
const myMap = new Map([ [a, 'a'], [ b, 'b']) //{{} => 'a', {} => 'b'}