Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. // MAPS //
  2.  
  3. const map = new Map()
  4. //
  5. map.set('key', 'key is a string')
  6. map.set({}, 'key is an object')
  7. map.set(function() {}, 'key is a function')
  8. map.set(true, 'key is a boolean')
  9. map.set(NaN, 'wat')
  10.  
  11. // OR
  12.  
  13. const map2 = new Map([
  14. ['key', 'key is a string'],
  15. [{}, 'key is an object'],
  16. [function() {}, 'key is a function'],
  17. [true, 'key is a boolean'],
  18. [NaN, 'wat'],
  19. ])
  20.  
  21. // OR
  22.  
  23. const map3 = new Map()
  24. .set('key', 'key is a string')
  25. .set({}, 'key is an object')
  26. .set(function() {}, 'key is a function')
  27. .set(true, 'key is a boolean')
  28. .set(NaN, 'wat')
  29.  
  30. //MAP METHODS
  31.  
  32. console.log('map size / length: ', map.size) // 5
  33. console.log('get value associated with a key (key === NaN):', map.get(NaN)) // 'wat'
  34. console.log('check to see if a key exists ("key"):', map.has('key')) // true
  35. console.log('list all keys:', map.keys())
  36. console.log('list all values:', map.values())
  37. console.log('return new iterator object', map.entries())
  38. console.log('delete a key / value pair', map2.delete(NaN)) // true
  39. console.log('Map after key deleted', map2)
  40. console.log('clear map', map2.clear()) // undefined
  41. console.log('map after being cleared', map2)
  42.  
  43. // ITERATING
  44.  
  45. // FOR OF
  46.  
  47. for (var [key, value] of map.entries()) {
  48. console.log(`key --> ${key},`)
  49. console.log(`value --> ${value}`)
  50. console.log('- - - - - - - - - - - -')
  51. }
  52.  
  53. // FOR EACH
  54.  
  55. map.forEach((value, key) => {
  56. console.log(`key --> ${key},`)
  57. console.log(`value --> ${value}`)
  58. console.log('- - - - - - - - - - -')
  59. })
  60.  
  61. // USING ITERATORS
  62.  
  63. const mapIterator = map.entries()
  64. const mapIterator2 = map.entries()
  65. const mapIterator3 = map[Symbol.iterator]()
  66.  
  67. console.log(
  68. 'map.entries creates a new iterator object each time its envoked',
  69. mapIterator === mapIterator2
  70. ) //false
  71.  
  72. console.log('entries is just an iterator', map[Symbol.iterator] === map.entries) //true
  73.  
  74. console.log(mapIterator.next().value)
  75. console.log(mapIterator.next().value)
  76. console.log(mapIterator.next().value)
  77.  
  78. console.log(mapIterator3.next().value)
  79. console.log(mapIterator3.next().value)
  80. console.log(mapIterator3.next().value)
  81.  
  82. // MAPS TO ARRAYS
  83.  
  84. console.log('keys to array -->', [...map.keys()])
  85. console.log('-------')
  86. console.log('values to array -->', [...map.values()])
  87. console.log('-------')
  88. console.log('map to array -->', [...map])
  89.  
  90. // MAPPING AND FILTERING
  91.  
  92. const filteredMap = new Map([...map].filter(([key, value]) => value === 'wat'))
  93. console.log('Filtered Map', filteredMap)
  94.  
  95. const mappedMap = new Map([...map].map(([key, value]) => [key, `${value}!`]))
  96. console.log('Mapped map', mappedMap)
  97.  
  98. // SETS //
  99.  
  100. const set = new Set()
  101.  
  102. set.add('one')
  103.  
  104. console.log('check for value', set.has('one'))
  105.  
  106. set.add(1).add({}).add(function() {}).add(NaN)
  107.  
  108. // or
  109.  
  110. const set2 = new Set(['one', 1, {}, function() {}, NaN])
  111.  
  112. console.log('sets can take arbitrary values', set)
  113.  
  114. console.log('Sets can easily be converted into an array', Array.from(set))
  115.  
  116. console.log('set handles NaN correctly', set.has(NaN))
  117.  
  118. const set3 = new Set().add(1).add(1).add(1).add(1).add(1)
  119. console.log('sets values must be unique', set3.size)
  120.  
  121. const set4 = new Set([{}, {}, {}, {}, {}])
  122. console.log('objects are different', set4.size)
  123.  
  124. //ITERATING
  125.  
  126. for (const x of set) {
  127. console.log('value: ', x)
  128. }
  129.  
  130. //MAPS AND FILTERS
  131.  
  132. const mappedSet = new Set([...set4].map(obj => (obj.key = obj)))
  133. console.log(mappedSet)
  134.  
  135. // Unions, intersections, and difference
  136.  
  137. const numSet1 = new Set([1, 2, 3, 4, 5])
  138. const numSet2 = new Set([3, 4, 5, 6, 7])
  139.  
  140. const union = new Set([...numSet1, ...numSet2])
  141. console.log('unions', union)
  142.  
  143. const intersection = new Set([...numSet1].filter(x => numSet2.has(x)))
  144. console.log('intersections', intersection)
  145.  
  146. const diff1 = new Set([...numSet1].filter(x => !numSet2.has(x)))
  147. const diff2 = new Set([...numSet2].filter(x => !numSet1.has(x)))
  148. console.log('difference one', diff1)
  149. console.log('difference two', diff2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement