「2次元配列を回転する」で、ハマっている。
<!DOCTYPE html> <title></title> <meta charset="UTF-8"> <body> <script type="application/javascript; version=1.8"> var ary = [ [1,2,3], [4,5,6], [7,8,9], ['a', 'b', 'c'] ]; function ArrayRotate (r) { switch ((4 + r % 4) % 4) { case 0 : break; case 1 : default : this.reverse (); var header = this[0]; var buf = header.map (function (a, b) { return [this.map (function (c) { return c[this]; }, b)]; }, this); Array.prototype.splice.apply (this, [0, this.length].concat (buf)); break; case 2 : this.reverse ().forEach (Array.reverse); break; case 3 : this.forEach (Array.reverse); var header = this[0]; var buf = header.map (function (a, b) { return [this.map (function (c) { return c[this]; }, b)]; }, this); Array.prototype.splice.apply (this, [0, this.length].concat (buf)); break; } } function toText (ary) { return ary.map (function (a) a.toString ()).join ('\n'); } //________________ ArrayRotate.call (ary, 1); // ary = ArrayRotate (ary, 1); //配列を返すのではなく、配列の元を変える。 alert (toText (ary)); </script> </body>
結局のところ this に対して代入できれば・・・
this.concat (123); //不可 this.push (123); //追加可能 this.splice (0, this.length); //削除可能 this.reverse (); //配列反転可
その2、配列内包を使う
それは、今はあきらめた。