예전에는 아래 방식이 좋다고 생각했었는데,
최근엔 그냥 json을 hashMap으로 써도 충분한것 같다. (좀 늦으려나?)
let hm = {}
hm['key1'] = 2
hm['key2'] = 3
==> 이렇게 하면 바로 hashMap처럼 사용가능. hm = {key1:2, key2:3} 이며, 이렇게 저장해도 됨.
--------------- 아래 --------------
REF 를 수정해서 만듦.
<javascript용 정수를 자동 add해주는 HashMap>
Map = function(){
this.map = new Object();
};
Map.prototype = {
put : function(key, value){
if (typeof this.get(key) == 'undefined')
this.map[key] = value;
else
this.map[key] = this.map[key]+value;
},
get : function(key){
return this.map[key];
},
//밑에 함수들은 사실 별필요 없음, 검증도 필요하고..
containsKey : function(key){
return key in this.map;
},
containsValue : function(value){
for(var prop in this.map){
if(this.map[prop] == value) return true;
}
return false;
},
isEmpty : function(key){
return (this.size() == 0);
},
clear : function(){
for(var prop in this.map){
delete this.map[prop];
}
},
remove : function(key){
delete this.map[key];
},
keys : function(){
var keys = new Array();
for(var prop in this.map){
keys.push(prop);
}
return keys;
},
values : function(){
var values = new Array();
for(var prop in this.map){
values.push(this.map[prop]);
}
return values;
},
size : function(){
var count = 0;
for (var prop in this.map) {
count++;
}
return count;
}
};
var map = new Map();
map.put("ag", 5);
map.put("ag", 2);
map.get("ag"); ===> 7