• ts 实现 Dictionary 字典


    非泛型版

    namespace demo {
      /**
       * 字典类
       * let dictionary = new Dictionary(); // new一个对象
       * // 设置属性
       * dictionary.Set('gandalf', 'gandalf@email.com');
       * dictionary.Set('john', 'johnsnow@email.com');
       * dictionary.Set('tyrion', 'tyrion@email.com');
       * // 调用
       * console.log(dictionary.Size());
       * console.log(dictionary.Values());
       * console.log(dictionary.Get('tyrion'));
       */
      export class Dictionary {
        /**
         * 字典项目
         */
        private items = {}
    
        /**
         * 验证指定键是否在字典中
         * @param key 键
         * @returns 是否存在
         */
        public Has(key: any): boolean {
          return key in this.items
        }
    
        /**
         * 设置键值
         * @param key 键
         * @param value 值
         */
        public Set(key: any, value: any): void {
          this.items[key] = value
        }
    
        /**
         * 移除指定键
         * @param key 键
         * @returns 是否移除成功
         */
        public Remove(key: any): boolean {
          if (this.Has(key)) {
            delete this.items[key]
            return true
          }
          return false
        }
    
        /**
         * 查找特定键的值
         * @param key 键
         * @returns 值
         */
        public Get(key: any): any {
          return this.Has(key) ? this.items[key] : undefined
        }
    
        /**
         * 获取字典所有的键
         * @returns 键数组
         */
        public Keys(): Array<any> {
          let values = new Array()//存到数组中返回
          for (let k in this.items) {
            if (this.Has(k)) {
              values.push(this.items[k])
            }
          }
          return values
        }
    
        /**
         * 获取字典所有的值
         * @returns 值数组
         */
        public Values(): Array<any> {
          // 存到数组中返回
          let values = new Array()
          for (let k in this.items) {
            if (this.Has(k)) {
              values.push(this.items[k])
            }
          }
          return values
        }
    
        /**
         * 获取所有键值
         * @returns 键值对对象
         */
        public GetItems(): object {
          return this.items
        }
    
        /**
         * 清空字典
         */
        public Clear(): void {
          this.items = {}
        }
    
        /**
         * 获取字典大小
         * @returns 
         */
        public Size(): number {
          return Object.keys(this.items).length
        }
      }
    }

    泛型版

    namespace demo {
      /**
       * 泛型字典类
       * let dictionary = new Dictionary<string>(); // new一个对象
       * // 设置属性
       * dictionary.Set('gandalf', 'gandalf@email.com');
       * dictionary.Set('john', 'johnsnow@email.com');
       * dictionary.Set('tyrion', 'tyrion@email.com');
       * // 调用
       * console.log(dictionary.Size());
       * console.log(dictionary.Values());
       * console.log(dictionary.Get('tyrion'));
       */
      export class GDictionary<T> {
        /**
         * 字典项目
         */
        private items = {}
    
        /**
         * 验证指定键是否在字典中
         * @param key 键
         * @returns 是否存在
         */
        public Has(key: string): boolean {
          return key in this.items
        }
    
        /**
         * 设置键值
         * @param key 键
         * @param value 值
         */
        public Set(key: string, value: T): void {
          this.items[key] = value
        }
    
        /**
         * 移除指定键
         * @param key 键
         * @returns 是否移除成功
         */
        public Remove(key: string): boolean {
          if (this.Has(key)) {
            delete this.items[key]
            return true
          }
          return false
        }
    
        /**
         * 查找特定键的值
         * @param key 键
         * @returns 值
         */
        public Get(key: string): T {
          return this.Has(key) ? this.items[key] : undefined
        }
    
        /**
         * 获取字典所有的键
         * @returns 键数组
         */
        public Keys(): Array<string> {
          let values = new Array<string>()//存到数组中返回
          for (let k in this.items) {
            if (this.Has(k)) {
              values.push(this.items[k])
            }
          }
          return values
        }
    
        /**
         * 获取字典所有的值
         * @returns 值数组
         */
        public Values(): Array<T> {
          // 存到数组中返回
          let values = new Array<T>()
          for (let k in this.items) {
            if (this.Has(k)) {
              values.push(this.items[k])
            }
          }
          return values
        }
    
        /**
         * 获取所有键值
         * @returns 键值对对象
         */
        public GetItems(): object {
          return this.items
        }
    
        /**
         * 清空字典
         */
        public Clear(): void {
          this.items = {}
        }
    
        /**
         * 获取字典大小
         * @returns 
         */
        public Size(): number {
          return Object.keys(this.items).length
        }
      }
    }
    嘴角上扬,记得微笑
  • 相关阅读:
    MySQL(2)---Explain
    MySQL(1)---索引
    php 的 PHPExcel1.8.0 使用教程
    通过html5 的EventSource来进行数据推送
    centos6.6 下 安装 php7 按 nginx方式
    IIS PHP Warning: Unknown: open(c:\php\tmp\sess_xxx, O_RDWR) failed: Permission denied (13) in Unknown on line 0
    动态加载JS,并执行回调函数
    nginx 504 gateway time out
    php 账号不能同时登陆,当其它地方登陆时,当前账号失效
    php 函数中静态变量的问题
  • 原文地址:https://www.cnblogs.com/jardeng/p/14849558.html
Copyright © 2020-2023  润新知