Simple javascript HashMap

function each(object, callback) {
	if (undefined === object.length) {
		for ( var name in object) {
			if (false === callback(object[name], name, object))
				break;
		}
	} else {
		for ( var i = 0, len = object.length; i < len; i++) {
			if (i in object) {
				if (false === callback(object[i], i, object))
					break;
			}
		}
	}
}

var Map = function() {
	this.keySet = new Array();
	this.values = new Array();
}

Map.prototype.put = function(mKey, mValues) {
	this.keySet.push(mKey);
	this.values.push(mValues);
}

Map.prototype.updateKeyValue = function(mKey, mValues) {
	if (this.keySet == null) {
		return null;
	}

	var hasKey = false, mIndex = -1;

	each(this.keySet, function(o, index) {
		if (mKey == o) {
			mIndex = index;
			hasKey = true;
			return false;
		}
	})

	if (hasKey == false) {
		this.keySet.push(mKey);
		this.values.push(mValues);
	} else {
		if (mIndex != -1) {
			this.values[mIndex] = mValues;
		}
	}
}

Map.prototype.get = function(mKey) {

	if (this.keySet == null) {
		return null;
	}

	for ( var i = 0; i < this.keySet.length; i++) {
		if (mKey == this.keySet[i]) {
			return this.values[i];
		}
	}

}

原文地址:https://www.cnblogs.com/wblade/p/1684627.html