title: JavaScript - Building of JSON data structure - How to change key name with variable value
body: <p>I am dynamically building a JSON data structure. My code works fine, but I have a problem, as I need to set the data up, then replace the key name.</p>
  
  <p>I need to replace the key name below with one of my variables - the actual request type itself that I have stored in a variable. </p>
  
  <p>I need to do this for the 'requestType' key below. It works fone for the values, but not for replacing the key name.</p>
  
  <p>Below is my code:</p>
  
  <pre><code>// Create data array, used for building request message
  var data = {
      requestType: { 
          item1 : null,
          item2 : null,
          item3 : null
      }
  };
  
  // Set the field array variables with data
  $('input[name="item1"], [name="item2"], [name="item3"]').each(function(index) {
      if(index==0){
          data.requestType.item1 = this.value;
      } else if(index==1){
          data.requestType.item2 = this.value;
      } else if(index==2){
          data.requestType.item3 = this.value;
      }
  }); 
  </code></pre>
  
  <p>Please help :)</p>