Created
February 11, 2014 17:41
-
-
Save mapsi/8939976 to your computer and use it in GitHub Desktop.
Convert JS array to HTML table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very helpful thought