Skip to content

Instantly share code, notes, and snippets.

@bteitelb
Last active December 12, 2015 06:08
Show Gist options
  • Save bteitelb/4726756 to your computer and use it in GitHub Desktop.
Save bteitelb/4726756 to your computer and use it in GitHub Desktop.
json2table - convert array of objects (as array or JSON) into HTML table rows and insert them into the given tbody
// Convert array of objects (as array or JSON) into HTML table rows and insert them into the given tbody
function json2table(tbody_id, json_or_array, array_of_column_keys) {
var tr, td, tbody = document.getElementById(tbody_id);
var objs = typeof(json_or_array) == 'string' ? JSON.parse(json_or_array) : json_or_array;
for (var i = 0; i < objs.length; i++) {
tr = tbody.insertRow(tbody.rows.length);
for (var j = 0; j < array_of_column_keys.length; j++) {
td = tr.insertCell(tr.cells.length);
td.innerHTML = objs[i][array_of_column_keys[j]];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment