node js之 将map类型转换为Json, 网上别用你那固定key的方法敷衍大家了。



这是一个网上的node js 封装Map 的方法。很好用

mapUtil.js


function Map(){
    this.elements = new Array();

    //获取Map元素个数
    this.size = function() {
        return this.elements.length;
    },

        //判断Map是否为空
        this.isEmpty = function() {
            return (this.elements.length < 1);
        },

        //删除Map所有元素
        this.clear = function() {
            this.elements = new Array();
        },

        //向Map中增加元素(key, value)
        this.put = function(_key, _value) {
            if (this.containsKey(_key) == true) {
                if(this.containsValue(_value)){
                    if(this.remove(_key) == true){
                        this.elements.push( {
                            key : _key,
                            value : _value
                        });
                    }
                }else{
                    this.elements.push( {
                        key : _key,
                        value : _value
                    });
                }
            } else {
                this.elements.push( {
                    key : _key,
                    value : _value
                });
            }
        },

//删除指定key的元素,成功返回true,失败返回false
        this.remove = function(_key) {
            var bln = false;
            try  {
                for (i = 0; i < this.elements.length; i++) {
                    if (this.elements[i].key == _key){
                        this.elements.splice(i, 1);
                        return true;
                    }
                }
            }catch(e){
                bln = false;
            }
            return bln;
        },

//获取指定key的元素值value,失败返回null
        this.get = function(_key) {
            try{
                for (i = 0; i < this.elements.length; i++) {
                    if (this.elements[i].key == _key) {
                        return this.elements[i].value;
                    }
                }
            }catch(e) {
                return null;
            }
        },

//获取指定索引的元素(使用element.key,element.value获取key和value),失败返回null
        this.element = function(_index) {
            if (_index < 0 || _index >= this.elements.length){
                return null;
            }
            return this.elements[_index];
        },

//判断Map中是否含有指定key的元素
        this.containsKey = function(_key) {
            var bln = false;
            try {
                for (i = 0; i < this.elements.length; i++) {
                    if (this.elements[i].key == _key){
                        bln = true;
                    }
                }
            }catch(e) {
                bln = false;
            }
            return bln;
        },

//判断Map中是否含有指定value的元素
        this.containsValue = function(_value) {
            var bln = false;
            try {
                for (i = 0; i < this.elements.length; i++) {
                    if (this.elements[i].value == _value){
                        bln = true;
                    }
                }
            }catch(e) {
                bln = false;
            }
            return bln;
        },

        //获取Map中所有key的数组(array)
        this.keys = function() {
            var arr = new Array();
            for (i = 0; i < this.elements.length; i++) {
                arr.push(this.elements[i].key);
            }
            return arr.sort();
        },

//获取Map中所有value的数组(array)
        this.values = function() {
            var arr = new Array();
            for (i = 0; i < this.elements.length; i++) {
                arr.push(this.elements[i].value);
            }
            return arr;
        };
}


在map里面放进一些数据:


map.put("a","b");



然后开始转换:

var jsonArray = {};
    
    for(var x in params.keys())
    {
        var key=params.keys()[x];
        var value=params.get(key);
        jsonArray[key]=value;
       
    }
    json=JSON.stringify(jsonArray);
    
    console.log("创建完毕之后的json"+json);


结果:{“a”:“b”}









exports.Map=Map;

node js之 将map类型转换为Json, 网上别用你那固定key的方法敷衍大家了。

上一篇:.NET/ASP.NETMVC 大型站点架构设计—迁移Model元数据设置项(自定义元数据提供程序)


下一篇:js控制台/console输出/打印图片