配列を表要素に変換する
文字列の先頭に "#" があると、TH要素に。
文字列を判断するのが、遠くにあるので効率が悪い。
<!DOCTYPE html> <title></title> <div id="test"> スクリプトが有効であればここに表が作成されます </div> <script type="application/javascript; version=1.8"> Array.prototype.shuffle = (function() { this.forEach (function (_, i, o) let (r = (Math.random() * (o.length - i) | 0) + i) [o[i], o[r]] = [o[r], o[i]]); return this; }); Array.prototype.rotate = (function () { let a = this, b = [], n = a.length; for (let i = 0; i < n; i++) for (let j = 0, c = b[i] = []; j < n; j++) c[j] = a[n - j - 1][i]; return b; }); const huga = (function (ary) ary.map (function (_, i, o) o.slice (i).concat (o.slice (0, i))) .shuffle ().rotate ().shuffle ()); const hoge = (function (hyo) (function (ary) { var doc = document; var dfg = doc.createDocumentFragment (); dfg.appendChild (doc.createElement ('table')) .appendChild (ary.reduce (hyo, doc.createElement ('tbody'))); return dfg; })) ((function (cell) (function (dfg, rows) let (tr = dfg.ownerDocument.createElement ('tr')) (dfg.appendChild (rows.reduce (cell, tr)), dfg))) (function (dfg, text) { let doc = dfg.ownerDocument; let tx = String (text); let node; if (0 <= tx.indexOf ('#')) { node = doc.createElement ('th'); text = tx.substring (1); } else node = doc.createElement ('td'); dfg.appendChild (node).appendChild (doc.createTextNode (text)); return dfg; })); const piyo = (function (ary, opt) { let totalAry = []; for (let idx = 0, len = ary.length; idx < len; idx++) { let cell = ary[idx]; let total = 0; for (let idx = 0, len = cell.length; idx < len; idx++) { let val = cell[idx]; if (! isNaN (val)) { total += +val; totalAry[idx] = (totalAry[idx] || 0) +val; } } ary[idx].push ('#' + total); } if (opt) { let total = totalAry.reduce (function (a, n) a + n); totalAry.push (total); totalAry.forEach (function (a,i,o) o[i] = '#' + a); ary.push (totalAry); } return ary; }); (function () { let ary = [0,1,2,3,4,5,6,7,8,9]; let hyo = huga (ary); let tbl = piyo (hyo, 1); let target = document.querySelector ('#test'); while (target.hasChildNodes ()) target.removeChild (target.firstChild); target.appendChild (hoge (tbl)); })(); </script>