js 中map集合
2017-06-26 00:00
134 查看
/** * map操作公共类 * @author:李凯旋 * @time: 2017年2月22日15:34:22 */ // 定义map function 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中取出对应的value DataMap.prototype.get = function(key) { try { return this.container[key]; } catch (e) { return e; } }; // 判断map中是否包含指定的key DataMap.prototype.containsKey = function(key) { try { for ( var p in this.container) { if (p == key) return true; } return false; } catch (e) { return e; } } // 判断DataMap中是否包含指定的value DataMap.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中指定的key DataMap.prototype.remove = function(key) { try { delete this.container[key]; } catch (e) { return e; } }; // 清空DataMap DataMap.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); 后台代码解析
相关文章推荐
- XML + XSL + JS 构建小型Web App (十四)
- Node.js(day5)
- 给初学者提供几本学习js值得看的书
- XML + XSL + JS 构建小型Web App (十三)
- XML + XSL + JS 构建小型Web App (十)
- XML + XSL + JS 构建小型Web App (十一)
- XML + XSL + JS 构建小型Web App (十二)
- XML + XSL + JS 构建小型Web App (八)
- XML + XSL + JS 构建小型Web App (九)
- XML + XSL + JS 构建小型Web App (二)
- XML + XSL + JS 构建小型Web App (三)
- XML + XSL + JS 构建小型Web App (四)
- XML + XSL + JS 构建小型Web App (五)
- XML + XSL + JS 构建小型Web App (六)
- XML + XSL + JS 构建小型Web App (七)
- XML + XSL + JS 构建小型Web App (一)
- ASP+JS时的"""
- XML + XSL + JS 构建小型Web App (十五)
- 真正的 用JS 做的 loading
- JS版常见问题(二)兼容版本的无提示关闭窗口!