字典又称关联数组,映射,是一个抽象的数据结构,它包含着类似的键值有序对

    字典是一种以键值对形式存储的数据结构
    特点是

    1. 非线性存储结构
    2. 字典中的元素是可变的,不是必须的,键值是不重复的
    3. 键值是可hash的

    手写字典类

    1. class Dict {
    2. constructor() {
    3. this.data = {}
    4. this.count = 0
    5. }
    6. add(key, value) {
    7. if (!this.data[key]) {
    8. this.count++
    9. }
    10. this.data[key] = value;
    11. return true;
    12. }
    13. remove(key) {
    14. if (this.data[key]) {
    15. delete this.data[key]
    16. this.count--
    17. }
    18. return true
    19. }
    20. update(key, newVal) {
    21. if (this.data[key]) {
    22. this.data[key] = newVal;
    23. } else {
    24. this.add(key, newVal)
    25. }
    26. }
    27. find(key) {
    28. return this.data[key]
    29. }
    30. showAll() {
    31. Object.keys(this.data).forEach(key => console.log(`${key}: ${this.data[key]}`))
    32. }
    33. total() {
    34. return this.count
    35. }
    36. clear() {
    37. this.data = {}
    38. this.count = 0
    39. }
    40. has(key) {
    41. return !!this.data[key]
    42. }
    43. sort() {
    44. let result = {}
    45. Object.keys(this.data).sort().forEach(key => {
    46. result[key] = this.data[key]
    47. })
    48. this.data = result
    49. }
    50. }
    51. // 实例化一个字典类
    52. const dic = new Dict();
    53. // 添加书籍和作者
    54. dic.add('书3', '作家3');
    55. dic.add('书4', '作家4');
    56. dic.add('书2', '作家2');
    57. dic.add('书1', '作家1');
    58. dic.add('书1', '作家2');
    59. /*
    60. 显示全部
    61. 结果为:
    62. 书3 --> 作家3
    63. 书4 --> 作家4
    64. 书2 --> 作家2
    65. 书1 --> 作家2
    66. */
    67. dic.showAll();
    68. // 打印字典元素总数 4
    69. console.log(dic.total())
    70. // 将字典排序
    71. dic.sort();
    72. dic.showAll();
    73. /*
    74. 将排序后的字典输出
    75. 结果为:
    76. 书1 --> 作家2
    77. 书2 --> 作家2
    78. 书3 --> 作家3
    79. 书4 --> 作家4
    80. */
    81. // 删除第四本书
    82. dic.remove('书4')
    83. // 打印字典元素总数 3
    84. console.log(dic.total())

    JS中的字典结构

    1. Object 是 键值对集合,但只能用字符串当做键
    2. Map提供一种完善的Hash结构,可以适用对象当键存储数据
      1. set/get/has/size/delete/clear
      2. keys/values/entries/forEach
      3. 键值可以hash ```javascript let map = new Map()

    let a = {} let b = {}

    map.set(a, 1) map.set(b, 2) console.log(map) // Map { {} => 1, {} => 2 }

    最后的输出值我们可以看到是有两个值,也就是说,我们 map 中对应的 a、b 的 key 值存储的是他们的哈希地址,并不是 {} 这个空对象。所以在 map 中才会有两个值存在

    1. 3. WeakMap
    2. 1. 只接受对象作为键,不包括null, 不接受其他类型的值为键
    3. 1. 不可遍历
    4. 1. 无法清空
    5. 1. 所引用的对象都是弱引用,垃圾回收是不将此引用考虑进去的
    6. <a name="XU10R"></a>
    7. ## 小结
    8. 1. 字典的特点和实现方式
    9. 1. js中的字典实现方式
    10. 1. Map WeakMap的不同
    11. <a name="RWpIR"></a>
    12. ## 练习题
    13. **统计所有小于非负整数 _n_ 的质数的数量。**<br />**<br />例如:输入10<br />输出:4<br />解释:小于 10 的质数一共有 4 2357
    14. ```javascript
    15. function count(n) {
    16. if (n <= 2) return 0;
    17. let result = new Array(n).fill(1)
    18. result[0] = 0
    19. result[1] = 0
    20. let count = 0;
    21. for (let i = 2; i < n; i++) {
    22. if (result[i] === 1) {
    23. count+=1
    24. }
    25. for (let j = 2; i * j < n; j++) {
    26. result[i * j] = 0
    27. }
    28. }
    29. return count
    30. }