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.prototype.reduce.call(table.rows, (acc, tr) => (
Array.prototype.forEach.call(tr.cells, (n, i) => (acc[i] ??= []).push(n)),
acc
), []);
table.replaceChildren();
cells.forEach(n => table.insertRow().append(...n));
}
https://jsfiddle.net/q4wy9s1o/