0

I have an ajax routine that returns data from an external source that is json encoded. The returned data is in a structure that includes an id value. I know what the expected id value is and store that in idVal. But I need to add this to the json data structure to obtain the content I want.

The following syntax is invalid but should make clear what I'm trying to do. Let's say that idVal is equal to 14:

$.ajax({
    url: theUrl,
    dataType: "json",
    success: function(results) {
        var data=results.content.id_[idVal].item;
        ...

So how do I reference "results.content.id_14.item" dynamically ?

AdamJones
  • 653
  • 1
  • 13
  • 35
  • Build the full property name and then use brackets notation, for instance ```results.content[`id_${idVal}`].item``` or ```results.content["id_" + idVal].item``` – T.J. Crowder Oct 18 '21 at 11:19

0 Answers0