js Map
// js 实现的 map 对象
// js中使用键值对一直是比较困难的,通过Array自定义方式来
// 实现js 中使用 map对象的,以此来达到js使用键值对的目的
// 使用示例
// 1. 引入 jsMap.js
// 2. 定义map对象 var map = new Map();
// 3. 操作map 对象
// 例如:map.put('key1','keyValue');
// map 支持的方法如下
// get(key) 获得key对应的value
// getAll() 获得所有的的value
// getAllKey() 获得所有的的key
// put(key,keyvalue)
// remove(key)
// size()
// isEmpty()
function Map() {
this.arr = new Array();
this.get = get;
this.getAll = getAll;
this.getAllKey = getAllKey;
this.put = put;
this.remove = remove;
this.size = size;
this.isEmpty = isEmpty;
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 getAll = function() {
var str="";
for (var i = 0; i < this.arr.length; i++) {
str += this.arr[i].value;
}
return str;
}
var getAllKey = function() {
var str="";
for (var i = 0; i < this.arr.length; i++) {
str += this.arr[i].key+";";
}
return str;
}
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);
}
}
this.arr_All = function(_index) {
if (_index < 0 || _index >= this.arr.length) {
return null;
}
return this.arr[_index];
};
var size = function() {
return this.arr.length;
}
var isEmpty = function() {
return this.arr.length <= 0;
}
}