首页 > 代码库 > js 构建map 和list
js 构建map 和list
//构建map function Map() { this.arr = new Array(); var struct = function(key, value) { this.key = key; this.value =http://www.mamicode.com/ value; }; this.keySet = function() { var arrKeys = new Array(); for(var i = 0; i < this.arr.length; i++) { arrKeys.push(this.arr[i].key); } return arrKeys; }; this.put = function(key, value){ for (var i = 0; i < this.arr.length; i++) { if ( this.arr[i].key === key ) { this.arr[i].value =http://www.mamicode.com/ value; return; } } this.arr[this.arr.length] = new struct(key, value); }; this.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; }; this.values=function(){ var value=http://www.mamicode.com/[] for (var i = 0; i < this.arr.length; i++) { value.push(this.arr[i].value); } return value.join(","); }; this.remove = function(key) { var v; for (var i = 0; i < this.arr.length; i++) { v = this.arr.pop(); if ( v.key === key ) { continue; } this.arr.unshift(v); } }; this.size = function() { return this.arr.length; }; this.isEmpty = function() { return this.arr.length <= 0; }; } /** * js实现list * */ function List() { this.value =http://www.mamicode.com/ []; /* 添加 */ this.add = function(obj) { return this.value.push(obj); }; /* 大小 */ this.size = function() { return this.value.length; }; /* 返回指定索引的值 */ this.get = function(index) { return this.value[index]; } /*返回指定对象的索引 */ this.indexOf = function(obj) { for ( var i in this.value) { if (obj == this.value[i]) { return i; } } }; /* 删除指定索引的值 */ this.remove = function(index) { this.value.splice(index,1); return this.value; }; /* 删除全部值 */ this.removeAll = function() { return this.value =http://www.mamicode.com/ []; }; /* 是否包含某个对象 */ this.constains = function(obj) { for ( var i in this.value) { if (obj == this.value[i]) { return true; } else { continue; } } return false; }; /* 是否包含某个对象 */ this.getAll = function() { var allInfos = ‘‘; for ( var i in this.value) { if(i != (value.length-1)){ allInfos += this.value[i]+","; }else{ allInfos += this.value[i]; } } alert(allInfos); return allInfos += this.value[i]+",";; }; }
js 构建map 和list
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。