Skip to content

Instantly share code, notes, and snippets.

@mapsi
Created February 11, 2014 17:41
Show Gist options
  • Save mapsi/8939976 to your computer and use it in GitHub Desktop.
Save mapsi/8939976 to your computer and use it in GitHub Desktop.
Convert JS array to HTML table
// build HTML table data from an array (one or two dimensional)
function generateTable(data) {
var html = '';
if (typeof (data[0]) === 'undefined') {
return null;
}
if (data[0].constructor === String) {
html += '<tr>\r\n';
for (var item in data) {
html += '<td>' + data[item] + '</td>\r\n';
}
html += '</tr>\r\n';
}
if (data[0].constructor === Array) {
for (var row in data) {
html += '<tr>\r\n';
for (var item in data[row]) {
html += '<td>' + data[row][item] + '</td>\r\n';
}
html += '</tr>\r\n';
}
}
if (data[0].constructor === Object) {
for (var row in data) {
html += '<tr>\r\n';
for (var item in data[row]) {
html += '<td>' + item + ':' + data[row][item] + '</td>\r\n';
}
html += '</tr>\r\n';
}
}
return html;
}
@RMPR
Copy link

RMPR commented Dec 2, 2018

Very helpful thought

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment