Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

9.JavaScript 数据结构与算法(九)字典 #9

Open
webVueBlog opened this issue Sep 13, 2022 · 0 comments
Open

9.JavaScript 数据结构与算法(九)字典 #9

webVueBlog opened this issue Sep 13, 2022 · 0 comments

Comments

@webVueBlog
Copy link
Member

webVueBlog commented Sep 13, 2022

字典

字典特点

  • 字典存储的是键值对,主要特点是一一对应
  • 比如保存一个人的信息
    • 数组形式:[19,"Tom", 1.65],可通过下标值取出信息。
    • 字典形式:{"age": 19, "name": "Tom", "height": 165},可以通过 key 取出 value
  • 此外,在字典中 key 是不能重复且无序的,而 Value 可以重复。

字典和映射的关系

  • 有些编程语言中称这种映射关系为字典,如 Swift 中的 Dictonary,Python 中的 dict
  • 有些编程语言中称这种映射关系为 Map,比如 Java 中的 HashMapTreeMap 等。

字典常见的操作

  • set(key,value) 向字典中添加新元素。
  • remove(key) 通过使用键值来从字典中移除键值对应的数据值。
  • has(key) 如果某个键值存在于这个字典中,则返回 true,反之则返回 false
  • get(key) 通过键值查找特定的数值并返回。
  • clear() 将这个字典中的所有元素全部删除。
  • size() 返回字典所包含元素的数量。与数组的 length 属性类似。
  • keys() 将字典所包含的所有键名以数组形式返回。
  • values() 将字典所包含的所有数值以数组形式返回。

字典封装

代码实现

// 字典结构的封装
export default class Map {
 constructor() {
   this.items = {};
 }
 // has(key) 判断字典中是否存在某个key
 has(key) {
   return this.items.hasOwnProperty(key);
 }
 // set(key, value) 在字典中添加键值对
  set(key, value) {
   this.items[key] = value;
  }
  // remove(key) 在字典中删除指定的key
  remove(key) {
   // 如果集合不存在该 key,返回false
   if (!this.has(key)) return false;
   delete this.items[key]
  }
  // get(key) 获取指定key的value,如果没有,返回undefined
 get(key) {
  return this.has(key) ? this.items[key] : undefined;
 }
 // 获取所有的key
 keys() {
   return Object.keys(this.items);
 }
 // 获取所有的value
 values() {
  return Object.values(this.items);
 }
 // size() 获取字典中的键值个数
 size() {
  return this.keys().length;
 }
 // clear() 清空字典中所有的键值对
 clear() {
  this.items = {}
 }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant