首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

用js兑现的Map,方便数据操作

2012-10-15 
用js实现的Map,方便数据操作可以像java中的Map一样使用,非常方便var dataMap new Map()js版Map实现文件

用js实现的Map,方便数据操作
可以像java中的Map一样使用,非常方便

var dataMap = new Map();


js版Map实现文件:
/** * js实现的map */// 定义mapfunction Map() {this.container = {};}// 将key-value放入map中Map.prototype.put = function(key, value) {try {if (key != null && key != "")this.container[key] = value;} catch (e) {return e;}};// 根据key从map中取出对应的valueMap.prototype.get = function(key) {try {return this.container[key];} catch (e) {return e;}};// 判断map中是否包含指定的keyMap.prototype.containsKey = function(key) {try {for (var p in this.container) {if (p == key)return true;}return false;} catch (e) {return e;}}// 判断map中是否包含指定的valueMap.prototype.containsValue = function(value) {try {for (var p in this.container) {if (this.container[p] === value)return true;}return false;} catch (e) {return e;}};// 删除map中指定的keyMap.prototype.remove = function(key) {try {delete this.container[key];} catch (e) {return e;}};// 清空mapMap.prototype.clear = function() {try {delete this.container;this.container = {};} catch (e) {return e;}};// 判断map是否为空Map.prototype.isEmpty = function() {if (this.keyArray().length == 0)return true;elsereturn false;};// 获取map的大小Map.prototype.size = function() {return this.keyArray().length;}// 返回map中的key值数组Map.prototype.keyArray = function() {var keys = new Array();for (var p in this.container) {keys.push(p);}return keys;}// 返回map中的value值数组Map.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;}

热点排行