As an example, I have 3 arrays with same length:
arrayOne = ['a', 'b', 'c']
arrayTwo = ['d', 'e', 'f']
arrayThree = ['g', 'h', 'i']
I wish to know if there is a way to populate a HTML5 table by columns using jQuery (or even if this is the best practice for things like this). So far, I've seen this post.
This post came very close to answering my question but it doesn't use arrays.
The ideal solution would be to populate the table by having the number of rows same as the length of any of the arrays. (Users will be able to choose the length of the array(s), therefore, the number of rows... I think I got carried away).
For example:
Before
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">First Column</th>
<th scope="col">Second Column</th>
<th scope="col">Third Column</th>
</tr>
</thead>
<tbody id="table-content">
</tbody>
</table>
Expected Output
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">First Column</th>
<th scope="col">Second Column</th>
<th scope="col">Third Column</th>
</tr>
</thead>
<tbody id="table-content">
<tr>
<td>a</td>
<td>d</td>
<td>g</td>
</tr>
<tr>
<td>b</td>
<td>e</td>
<td>h</td>
</tr>
<tr>
<td>c</td>
<td>f</td>
<td>i</td>
</tr>
</tbody>
</table>
The code I've tried was able to populate one of the columns, but I can't get any further.
const columnOne = arrayOne.map(p => {
const rowItem = $('<td>').html(p)
return $('<tr>').append(rowItem)
})
$('#table-content').append(columnOne)