静的に書き出した JS で配列をシャッフルする必要が出たので調べてみました。
こんな感じに書くといけるみたい。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Array.prototype.shuffle = function () { var i = this .length; while (i){ var j = Math.floor(Math.random()*i); var t = this [--i]; this [i] = this [j]; this [j] = t; } return this ; } var a = [1, 2, 3, 4, 5]; a.shuffle(); console.log(a); |