본문 바로가기
javascript

java map --> javascript map 구현

by 새로운 도전을 위한 한걸음 2015. 5. 10.
반응형

자바 맵을 스크립트로 구현

 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 <script>
  function JsMap()
  {
   this._array = new Array();//Map배열
   this.pointer = 0;

   this._getIndexByKey = function(key)
   {
    for(var i=0; i< this._array.length; i++)
    {
     if(key == this._array[i][0])
     {
      return i;
     }
    }
    return -1;
   }


   this.put = function(key,value)
   {
    var index = this._getIndexByKey(key)

    if(index == -1)
    {
     var newArray = new Array();//key와value를 담는 배열
     newArray[0] = key;
     newArray[1] = value;
     this._array[this._array.length] = newArray;
    }
    else
    {
     this._array[index][1] = value;
    }
   }

   this.get = function(key)
   {
   
    for(var i=0; i < this._array.length; i++)
    {
     if(this._array[i][0] == key)
      return this._array[i][1];
    }
   

   }

   this.isNext = function()
   {
    var result;
    if(this._array.length > this.pointer)
    {    
     result =  true;
    }
    else
    {
     result = false;
    }
    this.pointer++;
    return result;
   }

   this.size = function()
   {
    return this._array.length;
   }

   this.nowKey = function()
   {
    return this._array[this.pointer -1][0];
   }
   this.nowValue = function()
   {
    return this._array[this.pointer -1][1];
   }
  }

  function testF(){
 var mapTest = new JsMap();

 mapTest.put('1','test');
 mapTest.put('2','test1');
 mapTest.put('3','test2');
 mapTest.put('4','test3');
 

 var aaa =mapTest.get('4');
 alert(aaa);
  }

 </script>

 

 </HEAD>

 <BODY>
 <a href = "javascript:testF();">맵 테스트</a>
 </BODY>
</HTML>

반응형