-1

i need to add a variable to get result of my $.getJSON. I think if you see the Problem yiu know what i mean. It is not a big Problem i think my syntax is not correct. But i dont know how i can handle this.

function get_status() {
  let i;
  for (i = 0; i <= 1; i++) {
    $.getJSON('https://api.verystream.com/file/info?file=' + verystream_ids[i], function(data) { 
      var text = `Date: ${data.result.` + verystream_ids[i] + `.name}<br>`
      alert(text);
    });
  }
}

My problem is now here:

var text = `Date: ${data.result.` + verystream_ids[i] + `.name}<br>`

I need to add this variable verystream_ids[i] to my data.result.$VARIABLE$.name to get results. How can I add the variable correctly?

Here is a Picture of the JSON. IMG

  • Possible duplicate of [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – Herohtar May 14 '19 at 15:44

1 Answers1

1

To access the keys of an object by a variable you need to use bracket notation:

var text = `Date: ${data.result[verystream_ids[i]].name}<br>`;
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339