0

I have a function that injects user's data into html file

the json format is something like this:

{"Fname":"abc","LName":"cdf", "Phone":"123456";}

this is a function to inject data into html file:

function injectData(data){
    $.each(data, function(key, value) { 

    //notice that in the "data.value", I want 'value' to be a variable
    //so that when I loop over the array, data.value will become data.Fname,
    //and then data.LName and then data.Phone   
        $("#"+key).html(' ' + data.value +'');
});

How can I force javascript to interpret 'value' as a variable first before calling data.value in order to get the real value from JSON object? This is a little bit confusing. Hope you understand .Thank you

kaboom
  • 833
  • 4
  • 18
  • 38
  • 1
    Did you try `data[key]`? Possible duplicate of http://stackoverflow.com/questions/4255472/javascript-object-access-variable-property-name – Aiias Apr 07 '13 at 06:00

1 Answers1

0

Write something like:

for(key in data){ $("#"+key).html(' ' + data[key] +''); });
Ram
  • 143,282
  • 16
  • 168
  • 197
Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116