Something like this, assuming the first row is always the header (could certainly be changed to make it more flexible):
function getData(table, format) {
var rows = table.tBodies[0].rows,
header_row = rows[0],
result = [],
header = [],
format = format || function(val){return val;},
i, j, cell, row, row_data;
// extract header
for(i = 0, l = header_row.cells.length; i < l; i++) {
cell = header_row.cells[i];
header.push((cell.textContent || cell.innerText));
}
// extract values
for(i = 1, l = rows.length; i < l; i++) {
row = rows[i];
row_data = {};
for(j = 0, l = row.cells.length; j < l; j++) {
cell = row.cells[j];
row_data[header[j]] = format(i, j, cell.textContent || cell.innerText);
}
result.push(row_data);
}
return result;
}
Usage is:
var chartData = getData(referenceToTable, function(rowIndex, colIndex, value) {
return +value; // shortcut to format text to a number
});
By passing a format function, you can format the values of each cell into the right data type.
DEMO
It works under the assumption that there is only one tbody
element. You have to adjust it to your needs.