js 构建map 和list
Posted 成功的路上总是离不开贵人的帮助,名师的指点和小人的刺激。
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js 构建map 和list相关的知识,希望对你有一定的参考价值。
//构建map function Map() { this.arr = new Array(); var struct = function(key, value) { this.key = key; this.value = 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 = 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=[] 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 = []; /* 添加 */ 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 = []; }; /* 是否包含某个对象 */ 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的主要内容,如果未能解决你的问题,请参考以下文章
关于js中 .map()的问题,希望大神可以帮忙解读下这段代码的实现过程
js中如何取得jsp中的List;例如下边jsp中代码,代码简单写的,只要具体的解决方法,最好有代码实例