简体   繁体   中英

iterate through all datatable objects in a page?

I have a webpage that displays graphs using Google Charts. I've created multiple DataTables as below :

var rttldata1 = new google.visualization.DataTable();
var rttldata2 = new google.visualization.DataTable();
var rttldata3 = new google.visualization.DataTable();
...
var rttldata10 = new google.visualization.DataTable();

At a certain point, I want to do something to all of these tables with javascript.

Something like

for each (datatable){
    do something
}

Can some one point me in the right direction please?

Maybe try to push each datatable in an array and then map through the array?

var array = [];
array.push(rttldata1, rttldata2, rttldata3, ...., rttldatan);
array.map(function(datatable) { doSomething(datatable) });

First you will want to encapsulate the instantiation of each DataTable within a function to stay DRY and add to an array. Then you can loop through each object.

var tables = [];

function makeDataTable(options) {
  var options = options || {};
  var table = new google.visualization.DataTable(options);
  tables.push(table);
  return table; // not needed in this context, but you might want
}

// insert code that creates tables via makeDataTable({}) ...

for (var i = 0, max = tables.length; i < max; i += 1) {
  var currTable = tables[i];
  // do something with currTable
}

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