在Js中实现java中的Map, 支持Key为对象

function data() {
    var map = getMap();
    map.put({age: 24, name: "张三"}, ["张三"]);
    map.put("李四", ["李四"]);
    map.put({name: "张三", age: 23}, ["张三1"]);
    console.log(map.keys());
    console.log(map.size());
    map.remove({name: "张三", age: 23});
    console.log(map.keys());
    console.log(map.size());
    console.log(map.all());
    console.log(map.allList());
    map.clear();
    console.log(map.keys());
    console.log(map.size());
}
data();

function getMap() {
    
    var object = new Object();
    
    object.remove = function(key) {
        if (typeof key === 'object') {  
            var obj = this.buiderObjectSort(key);
            var keyValue = JSON.stringify(obj); 
            delete this[keyValue];
        }else {  
            delete this[key];
        }
    }
    
    object.clear = function() {
        for (var key in this) {  
            if (typeof this[key] === 'function') {  
                continue;
            }
            delete this[key]
        }
    }
    
    object.keys = function() {
        var arry1 = [];
        for (var key in this) {  
            if (typeof this[key] === 'function') {  
                continue;
            }
            try{
                var obj = JSON.parse(key);
                arry1.push(obj);
            }catch(e) {
                arry1.push(key);
            }
        }
        return arry1;
    }
    
    object.values = function() {
       var arry1 = [];
        for (var key in this) {  
            if (typeof this[key] === 'function') {  
                continue;
            }
            arry1.push(this[key]);
        }
        return arry1;
    }

    object.allList = function(keyName, valueName) {
        var arry1 = [];
        var keyN = keyName == undefined || keyName == null || keyName == '' ? 'key' : keyName;
        var valueN = valueName == undefined || valueName == null || valueName == '' ? 'value' : valueName;
        for (var key in this) {
            if (typeof this[key] === 'function') {
                continue;
            }
            try {
                var obj = JSON.parse(key);
                var object = {};
                object[keyN] = obj;
                object[valueN] = this[key];
                arry1.push(object);
            } catch (e) {
                var object = {};
                object[keyN] = key;
                object[valueN] = this[key];
                arry1.push(object);
            }
        }
        return arry1;
    }
    
    object.all = function() {
        var obj1 = new Object();
        for (var key in this) {  
            if (typeof this[key] === 'function') {  
                continue;
            }
            obj1[key] = this[key];
        }
        return obj1;
    }
    
    object.buiderObjectSort = function(obj) {
        var obj1 = new Object();
        var keys = Object.keys(obj);  
        keys.sort();  
        for(var i = 0 ; i < keys.length; i++) {
            var key = keys[i];
            obj1[key] = obj[key];
        }
        return obj1;
    }
    
    object.size = function() { 
       return Object.keys(this.getAll()).length;
    }  

    object.put = function(key, value) { 
        if (typeof key === 'object') {  
            var obj = this.buiderObjectSort(key);
            var keyValue = JSON.stringify(obj); 
            this[keyValue] = value;  
        }else {  
            this[key] = value;  
        }
    }  

    object.get = function(key) {  
        if (typeof key === 'object') {  
            var obj = this.buiderObjectSort(key);
            var keyValue = JSON.stringify(obj); 
            return this[keyValue];  
        }else {  
            return this[key];   
        }
    }  

    object.has = function(key) {  
        if (typeof key === 'object') {  
            var obj = this.buiderObjectSort(key);
            var keyValue = JSON.stringify(obj); 
            return (this.hasOwnProperty(keyValue));  
        }else {  
          return (this.hasOwnProperty(key));  
        }
    }  
    return object;
}

你可能感兴趣的:(javascript,java,前端)