当前位置: 代码迷 >> JavaScript >> js模拟Map对象,兑现key-value
  详细解决方案

js模拟Map对象,兑现key-value

热度:395   发布时间:2013-04-20 19:43:01.0
js模拟Map对象,实现key---value

根据java中map的属性,实现key----value保存

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

	var size = function () {
		return this.arr.length;
	}

	var isEmpty = function () {
		return this.arr.length <= 0;
	}
	this.arr = new Array();
	this.get = get;
	this.put = put;
	this.remove = remove;
	this.size = size;
	this.isEmpty = isEmpty;
}

?

?

  相关解决方案