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 | Show 5 more comments43 Answers
Reset to default 1 2 Next 183You 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
版权声明:本文标题:Rotate the elements in an array in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736706076a1948684.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
months[new Date().getMonth()]
to get the name of the current month? – Gumbo Commented Dec 31, 2009 at 12:59apply()
to make your implementation work – Christoph Commented Dec 31, 2009 at 13:24Dec
in the first position):["Dec", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov"]
– Jean Vincent Commented Dec 31, 2009 at 13:24Array.prototype.unshift.apply(this, this.splice(...))
- my version does the same thing, but usespush()
instead ofunshift()
– Christoph Commented Dec 31, 2009 at 19:00