admin管理员组

文章数量:1128740

I was wondering what was the most efficient way to rotate a JavaScript array.

I came up with this solution, where a positive n rotates the array to the right, and a negative n to the left (-length < n < length) :

Array.prototype.rotateRight = function( n ) {
  this.unshift( this.splice( n, this.length ) );
}

Which can then be used this way:

var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
months.rotate( new Date().getMonth() );

My original version above has a flaw, as pointed out by Christoph in the comments bellow, a correct version is (the additional return allows chaining):

Array.prototype.rotateRight = function( n ) {
  this.unshift.apply( this, this.splice( n, this.length ) );
  return this;
}

Is there a more compact and/or faster solution, possibly in the context of a JavaScript framework? (none of the proposed versions bellow is either more compact or faster)

Is there any JavaScript framework out there with an array rotate built-in? (Still not answered by anyone)

I was wondering what was the most efficient way to rotate a JavaScript array.

I came up with this solution, where a positive n rotates the array to the right, and a negative n to the left (-length < n < length) :

Array.prototype.rotateRight = function( n ) {
  this.unshift( this.splice( n, this.length ) );
}

Which can then be used this way:

var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
months.rotate( new Date().getMonth() );

My original version above has a flaw, as pointed out by Christoph in the comments bellow, a correct version is (the additional return allows chaining):

Array.prototype.rotateRight = function( n ) {
  this.unshift.apply( this, this.splice( n, this.length ) );
  return this;
}

Is there a more compact and/or faster solution, possibly in the context of a JavaScript framework? (none of the proposed versions bellow is either more compact or faster)

Is there any JavaScript framework out there with an array rotate built-in? (Still not answered by anyone)

Share Improve this question edited Jan 27, 2020 at 14:51 Ivar 6,76912 gold badges56 silver badges67 bronze badges asked Dec 31, 2009 at 12:44 Jean VincentJean Vincent 12.4k7 gold badges34 silver badges24 bronze badges 10
  • 1 I don’t get what your example should do. Why don’t you just use months[new Date().getMonth()] to get the name of the current month? – Gumbo Commented Dec 31, 2009 at 12:59
  • 2 @Jean: the code is broken: the way you do it, it'll unshift the spliced elements as an array and not individually; you'll have to use apply() to make your implementation work – Christoph Commented Dec 31, 2009 at 13:24
  • 1 Today the rotation would modify months to show this list (with Dec in the first position): ["Dec", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov"] – Jean Vincent Commented Dec 31, 2009 at 13:24
  • @Christoph, you are right, this would not work as a generic rotate function. It only works if used to convert to a string right after. – Jean Vincent Commented Dec 31, 2009 at 18:15
  • @Jean: you can fix your version via Array.prototype.unshift.apply(this, this.splice(...)) - my version does the same thing, but uses push() instead of unshift() – Christoph Commented Dec 31, 2009 at 19:00
 |  Show 5 more comments

43 Answers 43

Reset to default 1 2 Next 183

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

本文标签: Rotate the elements in an array in JavaScriptStack Overflow