js中生成map对象的方法_javascript技巧
程序员文章站
2022-03-11 20:06:53
...
复制代码 代码如下:
var Map = function(){
this._entrys = new Array();
this.put = function(key, value){
if (key == null || key == undefined) {
return;
}
var index = this._getIndex(key);
if (index == -1) {
var entry = new Object();
entry.key = key;
entry.value = value;
this._entrys[this._entrys.length] = entry;
}else{
this._entrys[index].value = value;
}
};
this.get = function(key){
var index = this._getIndex(key);
return (index != -1) ? this._entrys[index].value : null;
};
this.remove = function(key){
var index = this._getIndex(key);
if (index != -1) {
this._entrys.splice(index, 1);
}
};
this.clear = function(){
this._entrys.length = 0;;
};
this.contains = function(key){
var index = this._getIndex(key);
return (index != -1) ? true : false;
};
this.getCount = function(){
return this._entrys.length;
};
this.getEntrys = function(){
return this._entrys;
};
this._getIndex = function(key){
if (key == null || key == undefined) {
return -1;
}
var _length = this._entrys.length;
for (var i = 0; i var entry = this._entrys[i];
if (entry == null || entry == undefined) {
continue;
}
if (entry.key === key) {//equal
return i;
}
}
return -1;
};
this._toString = function(){
var string = "";
for (var i = 0; i string += this.getEntrys()[i].key+"::"+this.getEntrys()[i].value;
if(i!=this.getEntrys().length-1){
string += ";";
}
}
return string;
};
};
推荐阅读
-
使用JavaScript实现node.js中的path.join方法
-
原生js实现复制对象、扩展对象 类似jquery中的extend()方法教程
-
在JavaScript中重写jQuery对象的方法教程实例教程教程
-
js中console在一行内打印字符串和对象的方法
-
JS实现从对象获取对象中单个键值的方法示例
-
在JavaScript中操作数组之map()方法的使用
-
javascript实现Java中的Map对象功能的实例详解
-
js/jquery遍历对象和数组的方法分析【forEach,map与each方法】
-
JS中的forEach、$.each、map方法推荐(附实例)
-
JavaScript中的window对象的属性和方法;JavaScript中如何选取文档元素