Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MAPS //
- const map = new Map()
- //
- map.set('key', 'key is a string')
- map.set({}, 'key is an object')
- map.set(function() {}, 'key is a function')
- map.set(true, 'key is a boolean')
- map.set(NaN, 'wat')
- // OR
- const map2 = new Map([
- ['key', 'key is a string'],
- [{}, 'key is an object'],
- [function() {}, 'key is a function'],
- [true, 'key is a boolean'],
- [NaN, 'wat'],
- ])
- // OR
- const map3 = new Map()
- .set('key', 'key is a string')
- .set({}, 'key is an object')
- .set(function() {}, 'key is a function')
- .set(true, 'key is a boolean')
- .set(NaN, 'wat')
- //MAP METHODS
- console.log('map size / length: ', map.size) // 5
- console.log('get value associated with a key (key === NaN):', map.get(NaN)) // 'wat'
- console.log('check to see if a key exists ("key"):', map.has('key')) // true
- console.log('list all keys:', map.keys())
- console.log('list all values:', map.values())
- console.log('return new iterator object', map.entries())
- console.log('delete a key / value pair', map2.delete(NaN)) // true
- console.log('Map after key deleted', map2)
- console.log('clear map', map2.clear()) // undefined
- console.log('map after being cleared', map2)
- // ITERATING
- // FOR OF
- for (var [key, value] of map.entries()) {
- console.log(`key --> ${key},`)
- console.log(`value --> ${value}`)
- console.log('- - - - - - - - - - - -')
- }
- // FOR EACH
- map.forEach((value, key) => {
- console.log(`key --> ${key},`)
- console.log(`value --> ${value}`)
- console.log('- - - - - - - - - - -')
- })
- // USING ITERATORS
- const mapIterator = map.entries()
- const mapIterator2 = map.entries()
- const mapIterator3 = map[Symbol.iterator]()
- console.log(
- 'map.entries creates a new iterator object each time its envoked',
- mapIterator === mapIterator2
- ) //false
- console.log('entries is just an iterator', map[Symbol.iterator] === map.entries) //true
- console.log(mapIterator.next().value)
- console.log(mapIterator.next().value)
- console.log(mapIterator.next().value)
- console.log(mapIterator3.next().value)
- console.log(mapIterator3.next().value)
- console.log(mapIterator3.next().value)
- // MAPS TO ARRAYS
- console.log('keys to array -->', [...map.keys()])
- console.log('-------')
- console.log('values to array -->', [...map.values()])
- console.log('-------')
- console.log('map to array -->', [...map])
- // MAPPING AND FILTERING
- const filteredMap = new Map([...map].filter(([key, value]) => value === 'wat'))
- console.log('Filtered Map', filteredMap)
- const mappedMap = new Map([...map].map(([key, value]) => [key, `${value}!`]))
- console.log('Mapped map', mappedMap)
- // SETS //
- const set = new Set()
- set.add('one')
- console.log('check for value', set.has('one'))
- set.add(1).add({}).add(function() {}).add(NaN)
- // or
- const set2 = new Set(['one', 1, {}, function() {}, NaN])
- console.log('sets can take arbitrary values', set)
- console.log('Sets can easily be converted into an array', Array.from(set))
- console.log('set handles NaN correctly', set.has(NaN))
- const set3 = new Set().add(1).add(1).add(1).add(1).add(1)
- console.log('sets values must be unique', set3.size)
- const set4 = new Set([{}, {}, {}, {}, {}])
- console.log('objects are different', set4.size)
- //ITERATING
- for (const x of set) {
- console.log('value: ', x)
- }
- //MAPS AND FILTERS
- const mappedSet = new Set([...set4].map(obj => (obj.key = obj)))
- console.log(mappedSet)
- // Unions, intersections, and difference
- const numSet1 = new Set([1, 2, 3, 4, 5])
- const numSet2 = new Set([3, 4, 5, 6, 7])
- const union = new Set([...numSet1, ...numSet2])
- console.log('unions', union)
- const intersection = new Set([...numSet1].filter(x => numSet2.has(x)))
- console.log('intersections', intersection)
- const diff1 = new Set([...numSet1].filter(x => !numSet2.has(x)))
- const diff2 = new Set([...numSet2].filter(x => !numSet1.has(x)))
- console.log('difference one', diff1)
- console.log('difference two', diff2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement