自定义JS Map 函数

// 自定义JS Map 函数
function Map() {
var map = function (key, value) {//键值对
this.key = key;
this.value = value;
}
var put = function (key, value) {//添加键值对

this.arr[this.arr.length] = new map(key, value);
}
var remove = function (key) {//删除key="key"的键值对,返回value值
for (var i = 0; i < this.arr.length; i++) {
var temp = this.arr.pop();
if (this.arr[i].key === key) {

return this.arr[i].value;
}
this.arr.push(temp);
}
return null;
}
var getKey = function (value) {//返回key对应的value值
for (var i = 0; i < this.arr.length; i++) {
if (this.arr[i].value === value)
return this.arr[i].key;
}
return null;
}
var getValue = function (key) {//返回value对应的key值
for (var i = 0; i < this.arr.length; i++) {
if (this.arr[i].key === key)
return this.arr[i].value;
}
return null;
}
var getSize = function () {//返回容器大小
return this.arr.length;
}

var show = function () {//打印容器内容
var string = "";
for (var i = 0; i < this.arr.length; i++) {
string += (this.arr[i].key + ":" + this.arr[i].value + " ");
}
alert(string);
}
this.arr = new Array();
this.remove = remove;
this.put = put;
this.show = show;
this.getKey = getKey;
this.getValue = getValue;
this.getSize = getSize;

}

原文地址:https://www.cnblogs.com/90nice/p/9524192.html