I am currently trying to create a table from an array. The array is formatted like the following:
[
{
header: 'ID',
values: [1, 2]
},
{
header: 'First Name',
values: ['John', 'Jayne']
},
{
header: 'Last Name',
value: ['Doe', 'Doe']
}
]
I've been successful in creating the headers for the table but a bit stumped on where or how to approach the rendering from the body in such a fashion from this array.
const exampleArray = [
{
header: 'ID',
values: [1, 2]
},
{
header: 'First Name',
values: ['John', 'Jayne']
},
{
header: 'Last Name',
values: ['Doe', 'Doe']
}
];
const header = $('#results thead tr');
const body = $('#results tbody');
exampleArray.forEach((row) => {
header.append(
$(`<th>${row.header}</th>`)
);
});
<table id="results">
<thead>
<tr>
<!-- HEADERS -->
</tr>
</thead>
<tbody>
</tbody>
</table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
I'm unsure if this is the best kind of approach to take or whether it's worth mapping the result set into something more mutable before trying to render the table.