Skip to content

Instantly share code, notes, and snippets.

@karlazz
Created April 26, 2022 18:44
Show Gist options
  • Save karlazz/a28a57647c24bfca92e09400a0b1538e to your computer and use it in GitHub Desktop.
Save karlazz/a28a57647c24bfca92e09400a0b1538e to your computer and use it in GitHub Desktop.
Make a table from a javascript object
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