티스토리 뷰

프로그래밍

[JAVASCRIPT] MAP 사용

ReturnToHome 2016. 12. 24. 21:26

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;
     }
 };

 

var mapObj = new Map();

map.put("key", "value");

map.get("key");

 

--> java 에서 쓰는 것 처럼 사용하면 됨.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함