旋转JavaScript中数组中的元素
我想知道旋转JavaScript数组的最有效方法是什么。 我想出了这个解决方案,其中一个正数n将数组向右旋转,而一个负数n向左(-length < n < length): Array.prototype.rotateRight = function( n ) { this.unshift( this.splice( n, this.length ) ); } 然后可以使用这种方式: var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; months.rotate( new Date().getMonth() ); 正如克里斯托夫在下面的评论中指出的那样,我上面的原始版本有一个缺陷,那就是正确的版本(附加返回值允许链接): Array.prototype.rotateRight = function( n ) { this.unshift.apply( this, this.splice( n, …