Created
August 26, 2018 00:51
-
-
Save Anwesh43/657a84e9c1478c84d1d53eb464693a38 to your computer and use it in GitHub Desktop.
Json to table
This file contains hidden or 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
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