💾objects to HTML table
Last updated
Was this helpful?
Last updated
Was this helpful?
Was this helpful?
JS ⟩ value ⟩ object⟩ built-in ⟩ Array ⟩ example ⟩ objects to HTML table
turn an array of objects into a HTML <table> element.
replit ⟩ objects to HTML table, require ⟩ Node+ext
import {elem} from './ext/Node_ext.js';
// ⭐ objects to HTML table
function objectsToHTMLTable(objs, headers) {
// ⭐ table
return elem("table", table => {
💈範例:
import {} from './js/ext/Node_ext.js';
import {objectsToHTMLTable} from './js/objectsToHTMLTable.js';
// sample data
const data = [
{ name: "John", age: 25, city: "New York" },
{ name: "Mike", age: 32, city: "Los Angeles" },
{ name: "Sarah", age: 128, city: "Chicago" }
];
// append the table to the body
const table = objectsToHTMLTable(data, ['Name', 'Age', 'City']);
document.body.appendChild(table);
// text align ("age" column)
table.$all('tr').forEach(row => {
row.children[1].style.textAlign = 'right';
});