Skip to content

Instantly share code, notes, and snippets.

@NovoManu
Last active March 13, 2021 11:15
Show Gist options
  • Save NovoManu/6f61ba1fafb15259c850b21396108969 to your computer and use it in GitHub Desktop.
Save NovoManu/6f61ba1fafb15259c850b21396108969 to your computer and use it in GitHub Desktop.
// Map - data structure with key-value pairs, where keys can be any type
const map = new Map()
map.set('1', 'string')
.set(1, 'number')
.set(true, 'boolean')
const obj = { name: 'John' }
map.set(obj, 'John')
const symbol1 = Symbol()
const symbol2 = Symbol()
map.set(symbol1, 'Symbol 1')
.set(symbol2, 'Symbol 2')
console.log(map.get('1')) // Expected output: string
console.log(map.get(1)) // Expected output: number
console.log(map.get(true)) // Expected output: boolean
console.log(map.get(obj)) // Expected output: John
console.log(map.get(symbol1)) // Expected output: Symbol 1
console.log(map.get(symbol2)) // Expected output: Symbol 2
console.log(map.size) // Expected output: 6
console.log(map.has(1)) // Expected output: true
console.log(map.has(3)) // Expected output: false
map.delete(1)
console.log(map.size) // Expected output: 5
console.log(map.has(1)) // Expected output: false
// It's possible to iterate map with for/of loop
for (const [key, value] of map) {
console.log(key)
console.log(value)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment