jquery – Javascript access json data inside loop

Here, the pinName variable is being treated as a string, rather than as a property name. To access the properties of the list object using a variable, you can use the square bracket notation, like this:

for(var i=1; i<=2; i++) {
  var pinName = "dr" + i;
  
  console.log(list[pinName]);
}

This will access the properties of the list object using the value of the pinName variable as the property name. In the first iteration of the loop, this will be equivalent to list.dr1, and in the second iteration it will be equivalent to list.dr2.

Alternatively, you can use the Object.keys() method to loop through the properties of the list object, like this:

for (var key of Object.keys(list)) {
  console.log(list[key]);
}

Hope it helps.

Read more here: Source link