title: "Hydrate" a Javascript Object Instance with json array just like PHP does
body: <p>Here is the "problem", I fill PHP objects with data like this : </p>
  
  <pre><code>public function hydrate(array $data){
      foreach($data as $key=&gt;$value){
          $method = 'set'.ucfirst($key);
          if(METHOD_EXISTS($this,$method)){
              $this-&gt;$method($value);
          }
      }
  }
  </code></pre>
  
  <p>Now, i'd like to turn it to javascript object instead of PHP, so, i get the "data array" from my db with an .ajax call, and I get a JSON object that"looks" like this : </p>
  
  <pre><code>{"id":"44","name":"test","testKey":"qklfjmdjfq88"}
  </code></pre>
  
  <p>My Javascript "object" looks like this : </p>
  
  <pre><code>function myObject(){
      var element,id,name,testKey;
  
      this.hydrate = function(ajaxData){
          //THIS CODE DOES NOT WORK
          //THAT IS THE PROBLEM
          Object.keys(ajaxData).forEach(function(key){
              console.log("function loop key : "+key);//OK
              console.log("function loop value : "+ajaxData[key]);//OK
              this.key = Element[key];
          });
      };
  }
  </code></pre>
  
  <p>so, once I receive my JSON "array" in my js script, i'd like to turn it to a js object instance, so I'm doing this : </p>
  
  <pre><code>var myCurrentObject = new myObject();
  myCurrentObject.hydrate(/*WHAT MY AJAX CALL RETURN*/);
  console.log(myCurrentObject.name);
  //console shows undefined...
  </code></pre>
  
  <p>So problem seems to be in the .hydrate function... this.key does not correspond to the myObject instance var... (name / id / testKey) How to fill the js object instance with a function that looks like the one I use when working with php?</p>
  
  <p>Thanks for help/reading</p>