Created
April 26, 2022 18:44
-
-
Save karlazz/a28a57647c24bfca92e09400a0b1538e to your computer and use it in GitHub Desktop.
Make a table from a javascript object
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
function arrayToTable(data) { | |
const keys = [...data.reduce((all, obj)=>{ | |
Object.keys(obj).forEach(key => all.add(key)); | |
return all; | |
}, new Set())]; | |
const header = keys.map(key => `<th>${key}</th>`).join('') | |
const tbody = data.map(row => keys.map(key => `<td>${row[key]}</td>`).join('')).map(row => `<tr>${row}</tr>`) | |
return `<table> | |
<thead><tr>${header}</tr></thead> | |
<tbody>${tbody}</body> | |
</table>`; | |
}; | |
// https://stackoverflow.com/questions/17684201/create-html-table-from-javascript-object |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment