js中生成map对象的方法_javascript技巧
程序员文章站
2022-04-07 10:47:23
...
复制代码 代码如下:
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中循环遍历Array与Map的方法小结
-
js中json字符串转json对象的方法(提取json格式的数据)
-
js删除对象中的某个元素(js删除对象属性的方法)
-
JS中的forEach、$.each、map方法对比讲解
-
JavaScript中定义对象原型的两种使用方法
-
js中数组对象去重的两种方法
-
javascript中数组(Array)对象和字符串(String)对象的常用方法总结
-
使用JavaScript实现node.js中的path.join方法
-
原生js实现复制对象、扩展对象 类似jquery中的extend()方法教程
-
在JavaScript中重写jQuery对象的方法教程实例教程教程