Skip to content

Instantly share code, notes, and snippets.

@Anwesh43
Created August 26, 2018 00:51
Show Gist options
  • Save Anwesh43/657a84e9c1478c84d1d53eb464693a38 to your computer and use it in GitHub Desktop.
Save Anwesh43/657a84e9c1478c84d1d53eb464693a38 to your computer and use it in GitHub Desktop.
Json to table
function createTHead(keys) {
const tRow = keys.map((key) => `<th>${key}</th>`).join("")
return `<thead><tr>${tRow}</tr></thead>`
}
function createRow(obj) {
return Object.values(obj).map((ob) => `<td>${ob}</td>`).join("")
}
function createTable(jsonArray) {
const rowHtml = jsonArray.map((jsonObj) => `<tr>${createRow(jsonObj)}</tr>`).join("")
var html = ""
if (jsonArray.length > 0) {
html = `<table>${createTHead(Object.keys(jsonArray[0]))}<tbody>${rowHtml}</tbody></table>`
}
return html
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment