-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson2html.js
More file actions
21 lines (18 loc) · 720 Bytes
/
json2html.js
File metadata and controls
21 lines (18 loc) · 720 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
export default function json2html(data) {
// Extract all unique keys from the data to ensure all columns are represented
const headers = Array.from(new Set(data.flatMap(Object.keys)));
// Start building the HTML table string
let html = `<table data-user="skarthikeya962@gmail.com">\n<thead>\n<tr>`;
html += headers.map(header => `<th>${header}</th>`).join("");
html += `</tr>\n</thead>\n<tbody>\n`;
// Populate rows for each object in the data array
data.forEach(row => {
html += "<tr>";
headers.forEach(header => {
html += `<td>${row[header] || ""}</td>`;
});
html += "</tr>\n";
});
html += "</tbody>\n</table>";
return html;
}