Rotate the elements in an array in JavaScript Rotate the elements in an array in JavaScript arrays arrays

Rotate the elements in an array in JavaScript


You can use push(), pop(), shift() and unshift() methods:

function arrayRotate(arr, reverse) {  if (reverse) arr.unshift(arr.pop());  else arr.push(arr.shift());  return arr;}

usage:

arrayRotate([1, 2, 3, 4, 5]);       // [2, 3, 4, 5, 1];arrayRotate([1, 2, 3, 4, 5], true); // [5, 1, 2, 3, 4];

If you need count argument see my other answer:
https://stackoverflow.com/a/33451102 ๐Ÿ–ค๐Ÿงก๐Ÿ’š๐Ÿ’™๐Ÿ’œ


Type-safe, generic version which mutates the array:

Array.prototype.rotate = (function() {    // save references to array functions to make lookup faster    var push = Array.prototype.push,        splice = Array.prototype.splice;    return function(count) {        var len = this.length >>> 0, // convert to uint            count = count >> 0; // convert to int        // convert count to value in range [0, len)        count = ((count % len) + len) % len;        // use splice.call() instead of this.splice() to make function generic        push.apply(this, splice.call(this, 0, count));        return this;    };})();

In the comments, Jean raised the issue that the code doesn't support overloading of push() and splice(). I don't think this is really useful (see comments), but a quick solution (somewhat of a hack, though) would be to replace the line

push.apply(this, splice.call(this, 0, count));

with this one:

(this.push || push).apply(this, (this.splice || splice).call(this, 0, count));

Using unshift() instead of push() is nearly twice as fast in Opera 10, whereas the differences in FF were negligible; the code:

Array.prototype.rotate = (function() {    var unshift = Array.prototype.unshift,        splice = Array.prototype.splice;    return function(count) {        var len = this.length >>> 0,            count = count >> 0;        unshift.apply(this, splice.call(this, count % len, len));        return this;    };})();


I would probably do something like this:

Array.prototype.rotate = function(n) {    n = n % this.length;    return this.slice(n, this.length).concat(this.slice(0, n));}

Editย ย ย ย Hereโ€™s a mutator version:

Array.prototype.rotate = function(n) {    n = n % this.length;    while (this.length && n < 0) n += this.length;    this.push.apply(this, this.splice(0, n));    return this;}