JS 实现Map

	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());

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。