參考文章:javascript實(shí)現(xiàn)java的map對(duì)象
工作需要找的這個(gè)知識(shí)點(diǎn),覺(jué)得這篇文章寫得比較具體,之前被其他人坑了。
我調(diào)用了里面的put()、set()方法,發(fā)現(xiàn)put()方法有一個(gè)錯(cuò)誤,里面的值只能新增但是不能修改。改進(jìn)思路如下:
function?Map()?{??
this.elements?=?new?Array();?
//向MAP中增加元素(key,?value)???
this.put?=?function(_key,?_value)?{ ?
for(var i=0;i<this.elements.length;i++){
????//存在key則更新value
? ? if(this.elements[i].key == _key){
? ??????this.elements[i].value = value;
????????return false;
????}
}
//不存在key則新增key-value
?this.elements.push(?{??
????????????key?:?_key,??
????????????value?:?_value??
????????});??
????};??
}