// dynamic function to cteate table out of 2d arrays
function createTable(element, tableData) {
// creating table elements
var table = document.createElement('table');
// creating table body <tbody> element
var tableBody = document.createElement('tbody');
// creating rows based on first diamention datas
tableData.forEach(function(rowData) {
var row = document.createElement('tr');
// creating cells in each row based on second diamention datas
rowData.forEach(function(cellData) {
var cell = document.createElement('td');
// adding array item to it's cell
cell.appendChild(document.createTextNode(cellData));
// adding the cell to it's row
row.appendChild(cell);
});
// adding each row to table body
tableBody.appendChild(row);
});
// adding table body to table
table.appendChild(tableBody);
// adding table to document body
element.appendChild(table);
}
// example
createTable(
document.getElementById('my-table'),
[
['r1, c1', ' r1, c2'],
['r2, c1', ' r2, c2']
]
);