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
This function works in both way and works with any number (even with number greater than array length):
function arrayRotate(arr, count) {
count -= arr.length * Math.floor(count / arr.length);
arr.push.apply(arr, arr.splice(0, count));
return arr;
}
usage:
for(let i = -6 ; i <= 6 ; i++) {
console.log(arrayRotate(["