JSFiddle - React, Tailwind, and code Playground
HTML
<table id="table">
<tr>
<th>Type</th>
<th>Text</th>
<th>Time</th>
<th>Notification Time</th>
</tr>
<tr>
<td>Lab1</td>
<td>Some Text</td>
<td>Day of Week</td>
<td>Monday, Wednessday</td>
</tr>
<tr>
<td>Lab2</td>
<td>Some Text</td>
<td>Day of Week</td>
<td>Tuesday, Wednessday</td>
</tr>
</table>
<ul id="array">
</ul>
JavaScript
/**
* Provides simple functionality to convert a table to an array
*/
function ArrayParser() {
/**
* Converts a table column to an array
* @param {string} table jQuery selector for target table
* @param {string} column Should match th content
* @return {array} Array of td contents
*/
this.getArray = function (table, column) {
var node = $(table + ' th:contains(' + column+ ')'),
index = node.index();
return this.getValuesByIndex(table, index);
}
/**
* Finds table colums by index for all rows
* @param {string} table jQuery selector for target table
* @param {number} index The column index
* @return {array} Array of td contents
*/
this.getValuesByIndex = function (table, index) {
var nodes = $(table + ' td:nth-child(' + (index + 1) + ')'),
results = [];
nodes.each(function (key, value) {
results.push($(value).text());
});
return results;
}
}
//// Some testing code ////
function main() {
var parser = new ArrayParser(),
result = parser.getArray('#table', 'Type');
$.each(result, function (key, value) {
$('#array').append('<li>' + value + '</li>');
});
}
$(document).ready(function () {
main();
});