function transposeTable(table) {
const headerCol = table.rows[0]?.cells[1]?.tagName === 'TH';
const content = Array.from(
table.rows,
tr => Array.from(tr.cells, td => td.innerHTML)
);
table.innerHTML = content[0]?.map((n, i) => `
<tr>${content.map((m, j) => (j = (headerCol ? j : i) ? 'td' : 'th', `
<${j}>${m[i]}</${j}>`)).join('')}
</tr>
`).join('') ?? '';
}
или
function transposeTable(table) {
const cells = Array
.from(table.rows, tr => [...tr.cells])
.reduce((acc, n) => (
n.forEach((m, j) => (acc[j] ??= []).push(m)),
acc
), []);
Array.prototype.forEach.call(table.children, n => n.remove());
cells.forEach(n => table.insertRow().append(...n));
}
https://jsfiddle.net/mqjna81x/