zoukankan      html  css  js  c++  java
  • js数据结构Map -----字典

    // Map
            function Dictionary() {
                var items = {};
                this.has = function (key) {
                    return key in items;
                }
                this.set = function (key, value) {
                    items[key] = value; //{1}
                }
                this.remove = function (key) {
                    if (this.has(key)) {
                        delete items[key];
                        return true
                    }
                    return false
                }
                this.get = function (key) {
                    return this.has(key) ? items[key] : undefined;
                };
                this.values = function () {
                    var values = []
                    for (var k in items) { //{1}
                        if (this.has(k)) {
                            values.push(items[k]); //{2}
                        }
                    }
                    return values;
                };
                this.getItems = function () {
                    return items;
                }
                this.size = function(){
                    return Object.keys(items).length
                }
                this.keys = function(){
                    return Object.keys(items)
                }
    
            }
            var dictionary = new Dictionary();
        dictionary.set('Gandalf', 'gandalf@email.com');
        dictionary.set('John', 'johnsnow@email.com');
        dictionary.set('Tyrion', 'tyrion@email.com');
        console.log(dictionary.has('Gandalf'));
        console.log(dictionary.size());
        console.log(dictionary.keys());
        console.log(dictionary.values());
        console.log(dictionary.get('Tyrion'));
    

      

  • 相关阅读:
    mac安装搜狗
    idea的阿里代码规范检查
    记录windows10闪屏
    github访问不了
    线程安全问题例子
    简单负载均衡工具类
    git将远程分支回归到指定版本
    minio的使用
    php禁止浏览器使用缓存页面的方法
    百度seo
  • 原文地址:https://www.cnblogs.com/vali/p/9602981.html
Copyright © 2011-2022 走看看