Something like this, assuming the first line is always the headline (you could, of course, change 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; }
Using:
var chartData = getData(referenceToTable, function(rowIndex, colIndex, value) { return +value;
By passing a format function, you can format the values ββof each cell to the desired data type.
Demo
It works under the assumption that there is only one tbody element. You must adapt it to your needs.
source share