1

I am pulling data from a database in javascript using mysqljs

I can get the output:

{ 
  "Success": true,
  "Result": [{ 
    "buttonName": "90p"
  }, {
    "buttonName": "£1.90"
  }, {
    "buttonName": "£4.90"
  }]
}

Using:

document.getElementById("output").innerHTML = JSON.stringify(data);

However, how do I access each part of the array.

I have tried:

data[1].buttonName

Any help would be much appreciated.

Dez
  • 5,702
  • 8
  • 42
  • 51
Steven Helliwell
  • 77
  • 1
  • 1
  • 5

1 Answers1

0

You need to loop over that array Result.

For example: data.Result[index].buttonName

This example loops that array and appends new elements to the div with id output.

var data = { 
  "Success": true,
  "Result": [{ 
    "buttonName": "90p"
  }, {
    "buttonName": "£1.90"
  }, {
    "buttonName": "£4.90"
  }]
}

var output = document.getElementById("output");
data.Result.forEach(function(o) {
  var p = document.createElement('p');
  p.appendChild(document.createTextNode(o.buttonName));
  output.appendChild(p);
});
<div id='output'>
</div>
Ele
  • 33,468
  • 7
  • 37
  • 75