js中创建Map集合
function Map() { //创建一个map集合
//这里定义数据结构
var struct = function(key, value) {
this.key = key;
this.value = value;
}
//这是添加的方法
var put = function(key, value){
for (var i = 0; i < this.arr.length; i++) {
if ( this.arr[i].key === key ) {
this.arr[i].value = value;
return;
}
}
this.arr[this.arr.length] = new struct(key, value);
}
//获取的方法
var get = function(key) {
for (var i = 0; i < this.arr.length; i++) {
if ( this.arr[i].key === key ) {
return this.arr[i].value;
}
}
return null;
}
//这种是根据自己的需求来定义的方法
var getIndex = function(index){
if(this.arr.length-1>=index){
return this.arr[index].key;
}
return null;
}
//删除的方法
var remove = function(key) {
var v;
for (var i = 0; i < this.arr.length; i++) {
v = this.arr.pop();
if ( v.key*1 === key*1 ) {
continue;
}
this.arr.unshift(v);
}
}
//获取存储的数量
var size = function() {
return this.arr.length;
}
//判断是否为空
var isEmpty = function() {
return this.arr.length <= 0;
}
//清空
var toNull=function(){
this.arr=new Array();
}
//发布服务
this.arr = new Array();
this.get = get;
this.put = put;
this.toNull=toNull;
this.remove = remove;
this.getIndex =getIndex;
this.getValue=getValue;
this.size = size;
this.isEmpty = isEmpty;
}