自己写的Js有序的Map, 并且支持Key为对象

var  map  =  newMap();

map.put({name:"张三"}, 23);

map.put("李四", 24);

map.get({name:"张三"});

map.remove({name:"张三"});

map.put("张三", 23);

var  objArray  =  map.allList("name", "age");// [{name: "李四", age: 24}, {name: "张三", age: 23}]

map.values(); // [24, 23]

map.keys();// ["李四", "张三"]

map.has({name:"张三"});// false

map.size();// 2

var  obj  =  map.all(); // {"李四": 24, "张三": 23}

map.clear();

map.size();// 0

map.getIndex({name: "张三"}); // -1

        // 创建一个Map
        function newMap() {

            var objectMap = new Object();

            objectMap.mapArray = [];

            objectMap.sortIndexArray = [];

            objectMap.getKeyStr = function(key) {
                var nKey = key;
                if (typeof key === 'object') {
                    var obj = this.getBuiderObjectSort(key);
                    nKey = JSON.stringify(obj);
                }
                return nKey;
            }

            objectMap.getBuiderObjectSort = 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;
            }

            objectMap.getIndex = function(key) {
                var index = this.sortIndexArray.indexOf(this.getKeyStr(key));
                return index;
            }

            objectMap.remove = function(key) {
                var index = this.getIndex(key);
                if (index != -1) {
                    this.sortIndexArray.splice(index, 1);
                    this.mapArray.splice(index, 1);
                }
            }

            objectMap.clear = function() {
                this.mapArray = [];
                this.sortIndexArray = [];
            }

            objectMap.keys = function() {
                var arry1 = [];
                var self = this.sortIndexArray;
                for(var i = 0 ; i < self.length ; i++) {
                    try{
                        var obj = JSON.parse(self[i]);
                        arry1.push(obj);
                    }catch(e) {
                        arry1.push(self[i]);
                    }
                }
                return arry1;
            }

            objectMap.values = function() {
                var arry1 = [];
                var self = this.mapArray;
                for(var i = 0 ; i < self.length ; i++) {
                    arry1.push(self[i].value);
                }
                return arry1;
            }

            objectMap.allList = function(keyName, valueName) {
                var arry1 = [];
                var keyN = keyName == undefined || keyName == null || keyName == '' ? 'key' : keyName;
                var valueN = valueName == undefined || valueName == null || valueName == '' ? 'value' : valueName;
                var self = this.mapArray;
                for(var i = 0 ; i < self.length ; i++) {
                    try {
                        var obj = JSON.parse(self[i].key);
                        var object = {};
                        object[keyN] = obj;
                        object[valueN] = self[i].value;
                        arry1.push(object);
                    } catch (e) {
                        var object = {};
                        object[keyN] = self[i].key;
                        object[valueN] = self[i].value;
                        arry1.push(object);
                    }
                }
                return arry1;
            }

            objectMap.all = function() {
                var obj1 = new Object();
                var self = this.mapArray;
                for(var i = 0 ; i < self.length ; i++) {
                    var key = self[i].key;
                    obj1[key] = self[i].value;
                }
                return obj1;
            }

            objectMap.size = function() {
                return this.mapArray.length;
            }

            objectMap.put = function(key, value) {
                var nKey = this.getKeyStr(key);
                var index = this.getIndex(key);
                if (index != -1) {
                    this.mapArray[index].value = value;
                }else{
                    this.mapArray.push({key: nKey, value: value});
                    this.sortIndexArray.push(nKey);
                }
            }

            objectMap.get = function(key) {
                var index = this.getIndex(key);
                if (index != -1) {
                    return this.mapArray[index].value;
                }
                return null;
            }

            objectMap.has = function(key) {
                var index = this.getIndex(key);
                return index != -1;
            }
            return objectMap;
        } 

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