JS实现MAP
Map.js代码如下:
function Map(linkItems) {
this.current = undefined;
this._size = 0;
if(linkItems === false){
this.disableLinking();
}
}
/**
* 获取当前map
* @return 当前对象
*/
Map.noop = function() {
return this;
};
/**
* 非法操作
* @return
*/
Map.illegal = function() {
throw new Error("非法操作,Map已经被禁用");
};
/**
*
* @param obj
* @param foreignKeys
* @return
*/
Map.from = function(obj, foreignKeys) {
var map = new Map;
for(var prop in obj) {
if(foreignKeys || obj.hasOwnProperty(prop)){
map.put(prop, obj[prop]);
}
}
return map;
};
/**
* 禁用map
* @return
*/
Map.prototype.disableLinking = function() {
this.link = Map.noop;
this.unlink = Map.noop;
this.disableLinking = Map.noop;
this.next = Map.illegal;
this.key = Map.illegal;
this.value = Map.illegal;
this.clear = Map.illegal;
return this;
};
/**
* 返回hash值 例如:number 123
* @param value key/value
* @return
*/
Map.prototype.hash = function(value) {
return (typeof value) + ' ' + (value instanceof Object ? (value.__hash || (value.__hash = ++arguments.callee.current)) : value.toString());
};
/**
* 返回map的长度
* @return
*/
Map.prototype.size = function() {
return this._size;
};
Map.prototype.hash.current = 0;
/**
* 通过key获取value
* @param key
* @return
*/
Map.prototype.get = function(key) {
var item = this[this.hash(key)];
return item === undefined ? undefined : item.value;
};
/**
* 向map中添加数据
* @param key 键
* @param value 值
* @return
*/
Map.prototype.put = function(key, value) {
var hash = this.hash(key);
if(this[hash] === undefined) {
var item = { key : key, value : value };
this[hash] = item;
this.link(item);
++this._size;
}else{
this[hash].value = value;
}
return this;
};
/**
* 通过key删除数据
* @param key
* @return
*/
Map.prototype.remove = function(key) {
var hash = this.hash(key);
var item = this[hash];
if(item !== undefined) {
--this._size;
this.unlink(item);
delete this[hash];
}
return this;
};
/**
* 清除map
* @return
*/
Map.prototype.clear = function() {
while(this._size){
this.remove(this.key());
}
return this;
};
/**
* 处理队列
* @param item
* @return
*/
Map.prototype.link = function(item) {
if(this._size == 0) {
item.prev = item;
item.next = item;
this.current = item;
}else {
item.prev = this.current.prev;
item.prev.next = item;
item.next = this.current;
this.current.prev = item;
}
};
Map.prototype.unlink = function(item) {
if(this._size == 0){
this.current = undefined;
}else {
item.prev.next = item.next;
item.next.prev = item.prev;
if(item === this.current){
this.current = item.next;
}
}
};
/**
* 获取下一个
* @return
*/
Map.prototype.next = function() {
this.current = this.current.next;
return this;
};
/**
* 获取key
* @return
*/
Map.prototype.key = function() {
return this.current.key;
};
/**
* 获取value
* @return
*/
Map.prototype.value = function() {
return this.current.value;
};
测试代码如下:var l=10000;
var map=new Map();
var start=new Date().getTime();
for(var i=0;i<l;i++){
map.put("key_"+i,new Date());
}
var end=new Date().getTime();
document.write("向map中添加了 "+l+" 个Date对象..........");
document.write("<br/>");
document.write("耗时 "+(end-start)+" 毫秒,map的长度为:"+map.size());
document.write("<br/>");
document.write("在map中提取全部数据..........");
document.write("<br/>");
start=new Date().getTime();
for(var i=0;i<map.size();i++){
map.get("key_"+i).getTime();
}
end=new Date().getTime();
document.write("耗时 "+(end-start)+" 毫秒");
document.write("<br/>");
document.write("清空map..........");
document.write("<br/>");
start=new Date().getTime();
map.clear();
end=new Date().getTime();
document.write("耗时 "+(end-start)+" 毫秒,map的长度为:"+map.size());
document.write("<br/>");
使用next:
var map=new Map();
map.put("key_1","value_1");
map.put("key_2","value_2");
map.put("key_3","value_3");
var m=map.next();
document.write("map.next:key="+m.key()+" value="+m.value());
document.write("<br/>");
m=map.next();
document.write("map.next:key="+m.key()+" value="+m.value());
结果:
map.next:key=key_2 value=value_2
map.next:key=key_3 value=value_3
分享到:
相关推荐
js Map 对象,js map ,map ,键值对
* Map对象,实现map功能 * put(key, value) 向map中增加键值对 * 例子: * var map = new Map(); * * map.put("key", "value"); * var val = map.get("key");
里面包含两个实例,js实现map的相关功能
使用js实现map工具类
javascript实现map,极其方便,可在需要当前页处理多数据时使用
NULL 博文链接:https://hoochiang.iteye.com/blog/1688307
里面有三个js文件,都是需要引入的。具体的方法,可以看源码。
javascript 模拟 java中的 List,Map js文件为 js/utils.js IE6.0 测试通过 <br>List: add(var obj) //添加一个元素 remove(var index) //删除一个元素 get(var index) //获取一个元素 remove...
自己设计的用JavaScript实现Array 和Map 里面有三个Js文件,都需要引入.
部分IE浏览器不支持ecmascript 6,无法使用map对象,用原生js对象实现map功能
js遍历map javaScript遍历map的简单实现 var map = { "name" : "华仔", "realname":"刘德华" }; for (var key in map) { console.log("map["+key+"]"+map[key]); } 这样会把map给遍历掉,显示在浏览器上的...
通过JS的使用,实现类似于JAVA中Map的操作
高性能的 javascript map实现,稳定性强的 基于 JSON的map! *同时附赠网络上搜索的map一个,用于对比。 经过笔者测试,网络上的map不如笔者所写的map性能高,容量也相差很多。
原创 在javascript中,仅提供了数组(Array)对象,本文利用数组对象来模式实现MAP对象
前几天项目上想用map集合一样的东西,简单拿对象拼了一下子,今天闲的慌实现一下 大家不要见笑 代码 var Map = function (){ /************基础变量**************/ var hashmap = {}; var keys = []; var vals...
主要介绍了JavaScript中利用Array和Object实现Map的方法,实例分析了javascript实现map的添加、获取、移除、清空、遍历等操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下
附件为map.js和测试html页面 博文链接:https://dote.iteye.com/blog/1756636
一个使用map实现web缓存的工具类 可供web应用开发参考
本文实例讲述了JS实现的自定义map方法。分享给大家供大家参考,具体如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>www.jb51.net JS自定义map方法&...