码迷,mamicode.com
首页 > Web开发 > 详细

js中创建Map集合

时间:2018-06-06 22:10:04      阅读:429      评论:0      收藏:0      [点我收藏+]

标签:val   方法   on()   sem   定义   new   remove   shift   判断   

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;
}

js中创建Map集合

标签:val   方法   on()   sem   定义   new   remove   shift   判断   

原文地址:https://www.cnblogs.com/xielinjiang/p/9146220.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!