JS 实现Map

页面导航:首页 > 网络编程 > JavaScript > JS 实现Map

JS 实现Map

来源: 作者: 时间:2016-02-20 09:49 【

function Map() {this arr = new Array();var struct = function(key, value) {this key = key;this value = value; };this put = function(key, value){ for (var
	function Map() {
		this.arr = new Array();
		var struct = function(key, value) {
				this.key = key;
				this.value = value;
	    };
		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;
		 };
	}
	var map = new Map();
	map.put("a","aaaaaaaaaaaaaaa");
	map.put("b","bbbbbbbbbbb");
	map.put("c","cccccccccccccc");
	map.remove("b");
	console.info(map.values());

Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<