/**
*
* 說明:js實作的map方法
* @returns {Map}
*/
function Map() {
var struct = function(key, value) {
this.key = key;
this.value = 值;
};
//新增map鍵值對
var put = function(key, value){
for (var i = 0; i if ( this.arr[i].key === key ) {
this.arr[i].value = value;
返回;
}
};
this.arr[this.arr.length] = new struct(key, value);
};
//依key取得value
var get = function(key) {
for (var i = 0; i if ( this.arr [i].key === key ) {
return this.arr[i].value;
}
}
回傳null;
};
//依key刪除
var remove = function(key) {
var v;
for (var i = 0; i v = this. arr.pop();
if ( v.key === key ) {
繼續;
}
this.arr.unshift(v);
}
};
//取得map鍵值對個數
var size = function() {
return this.arr.length;
};
//判斷map是否為空
var isEmpty = function() {
return this.arr.length };
this.arr = new Array();
this.get = get;
this
this .put = put;
this.remove = 刪除;
this.size = 大小;
this.isEmpty = isEmpty;
}
使用方法和java中Map類別同
程式碼如下:
var map=new Map();
map.put("num",1);
map.put("ss","wss");
警報(map.size ());
alert(map.get("num"));
paraArr.remove("num");
警報(map.size());
alert(map. get("num"));
腳本>