/** * map操作公共类 * @author:李凯旋 * @time: 2017年2月22日15:34:22 */// 定义mapfunction DataMap() { this.container = {};}// 将key-value放入map中DataMap.prototype.put = function(key, value) { try { if (key != null && key != "") this.container[key] = value; } catch (e) { return e; }};// 根据key从map中取出对应的valueDataMap.prototype.get = function(key) { try { return this.container[key]; } catch (e) { return e; }};// 判断map中是否包含指定的keyDataMap.prototype.containsKey = function(key) { try { for ( var p in this.container) { if (p == key) return true; } return false; } catch (e) { return e; }}// 判断DataMap中是否包含指定的valueDataMap.prototype.containsValue = function(value) { try { for ( var p in this.container) { if (this.container[p] === value) return true; } return false; } catch (e) { return e; }};// 删除DataMap中指定的keyDataMap.prototype.remove = function(key) { try { delete this.container[key]; } catch (e) { return e; }};// 清空DataMapDataMap.prototype.clear = function() { try { delete this.container; this.container = {}; } catch (e) { return e; }};// 判断DataMap是否为空DataMap.prototype.isEmpty = function() { if (this.keyArray().length == 0) return true; else return false;};// 获取DataMap的大小DataMap.prototype.size = function() { return this.keyArray().length;}// 返回DataMap中的key值数组DataMap.prototype.keyArray = function() { var keys = new Array(); for ( var p in this.container) { keys.push(p); } return keys;}// 返回DataMap中的value值数组DataMap.prototype.valueArray = function() { var values = new Array(); var keys = this.keyArray(); for (var i = 0; i < keys.length; i++) { values.push(this.container[keys[i]]); } return values;}2.用法1.折样的类型,声明为map的需要两步操作var showData = new DataMap();1.var jsonObj = showData.container;2.var jsonStr = JSON.stringify(jsonObj); 2.如果是直接声明为集合的var attData = {};可以直接转换,一步操作var jsonStr = JSON.stringify(attData); 后台代码解析