JavaScript & HTML
javascript Map HashMap (jquery)
husks
2017. 5. 19. 17:02
반응형
Javascript에서 Map을 사용할때 사용하는 선언 입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | Map = function(){ this.map = new Object(); }; Map.prototype = { put : function(key, value){ 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; } }; |
사용법
1 2 3 | var map = new Map(); map.put("id", "test"); map.get("id"); |
반응형