简体   繁体   中英

Way to change reference variable for jsondata

I want to reference the elements inside a 'parsed' JSON data object jsonData .]. I can reference the element by putting the actual name, same as in the example below. If I don't know which element to reference, how can I use a variable to refer to multiple elements?

This is my code:

var received = {};
received = search_options();
var test = received['data']; //variable prepared for replacing actual element name

var data = new google.visualization.DataTable();
data.addColumn('string', 'timeline');
data.addColumn('number', 'solid_t2');
var dataArray = [];
var jsonData = $.parseJSON(data1);
for (var i = 0; i < jsonData.length; i++) {
    dataArray.push([jsonData[i].timeline.substring(0, 5), parseInt(jsonData[i].solid_t2)]); 
    // How can I replace 'solid_t2'(above line) with a variable?
}
jsonData[i][test]

I personally prefer to save references before accessing a multidimensional array for better readability.

var firstLevelElementOfJson = jsonData[i];
requiredSecondLevelElement = firstLevelElementOfJson[ test ];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM